Atozed Forums

Full Version: C++ example for cache system to send file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello there

I need to update some old code.  It currently generates a PDF file and saves it to the UserCacheDir and then uses SendFile to download the file for the user.

The above no longer works in an ISAPI release build, after some research I've found that the cache system is what should be used now for this.  I've been unable to locate a C++ example of using the cache system to send a file.

Any help appreciated

Thanks

Gav
Please check this new demo:

https://github.com/Atozed/IntraWeb/tree/...eTimeCache

It is the same project named OneTimeCache from Delphi demo repository, converted to C++ Builder.

It should get you familiarized with the new Cache feature and how to get started.

Please let me know if you have any other questions.
Hello there

Thank you for the example code. I've had it working using the server build. So I switched over to my ISAPI project where it also worked in debug mode.

However, when I do a release build, the file doesn't get served - It appears that the path I'm using doesn't exist, so please advise which

In debug inside builder, when I create my PDF file this is the location...
C:\Users\icaru\AppData\Local\Temp\01d2hlems0\user\TQpucrrKel5-GK9lReBKX05yXdq

In release outside of Builder, when I create my PDF file this is the location...
C:\WINDOWS\TEMP\01d2hlyb7v\user\wbKedcHYwhsQH4blNu6GiBdRJaa

I added a directory exists check and in debug this passes, but in release it doesn't. What should I be using in release mode?

Here's my example code...

// Path for generated file and file name for the file
AnsiString sPath = WebApplication->UserCacheDir;
AnsiString sPathFile = sPath + String("contents.pdf");

// test message for release build testing, confirm generated file location
WebApplication->ShowMessage(sPath);

// test message for release build testing, check directory
WebApplication->ShowMessage("directory exist check");

if (System::Sysutils:Big GrinirectoryExists(sPath))
WebApplication->ShowMessage("directory exists");
else
WebApplication->ShowMessage("directory does NOT exist")

// Object for creating the PDF file
TContentsScreenPDF* oPDFIt = new TContentsScreenPDF(NULL, oSession->oSys, oSession->oDM, oContentsList);

// test message for release build testing
WebApplication->ShowMessage("generate PDF");

// Generate PDF
if (oPDFIt->Export(sPathFile.c_str(), "Contents Report")) {
// test message, confirm generate worked
WebApplication->ShowMessage("PDF created");

// PDF generated, copy it to cache
String sTempFileName = TIWAppCache::NewTempFileName();
String sFileUrl = oSession->WebApplication->CacheFiles->Add(sFileName);
FileCopy(sPathFile, sTempFileName, true);

String sURL = TIWAppCache::AddFileToCache(this, sTempFileName, TIWMimeTypes::GetAsString(mtPDF), ctOneTime);
// Serve up the file
WebApplication->NewWindow(sURL);
}
Hi, try something like this:
Code:
// get a new temp file name. This method only returns a file name, the file is not created
System::UnicodeString xFileName = TIWAppCache::NewTempFileName();

// Generate PDF using new temp file name created above.
if (oPDFIt->Export(xFileName.c_str(), "Contents Report")) {
  // add the pdf file to the cache. cache type is defined as ctOneTime, i.e., the file will be deleted when served
  UnicodeString xURL = TIWAppCache::AddFileToCache(this, xFileName, TIWMimeTypes::GetAsString(mtPDF), ctOneTime);
  // open a new window with your PDF file
  WebApplication->NewWindow(xURL);
}
Hi Jose

Sorry, somehow I posted my reply above before actually finishing what I was putting together.

I thought I'd tried what you shared, but guess I missed something off and went off down the rabbit hole.  That appears to work, so I'll test with other reports.

Thanks for your help