Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Naming a cache file for download


This question is answered.


Permlink Replies: 1 - Last Post: Feb 22, 2017 12:22 PM Last Post By: Alexandre Machado
Davide Rubbiani

Posts: 70
Registered: 8/15/03
Naming a cache file for download  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 22, 2017 9:06 AM
Hello,

I'm working with Berlin 10.1 update 2 e IW 14.1.6.
I'm developing an application where user uploaded files are saved in database blob field.
When user navigate to the download page the "clicked" file is retrieved from the database and through TIWAppCache.StreamToCacheFile is served in a new window.
Cause the files are .pdf and users can download it I would like to serve the file with its original file name (that I saved in the database) instead of the cache file name.
How can I give a name to a cache file?
Or is it possible to create a cache file with a specified name?

Thank you,
Davide
Alexandre Machado

Posts: 1,754
Registered: 8/10/13
Re: Naming a cache file for download
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 22, 2017 12:19 PM   in response to: Davide Rubbiani in response to: Davide Rubbiani
Davide Rubbiani wrote:
Hello,

I'm working with Berlin 10.1 update 2 e IW 14.1.6.
I'm developing an application where user uploaded files are saved in database blob field.
When user navigate to the download page the "clicked" file is retrieved from the database and through TIWAppCache.StreamToCacheFile is served in a new window.
Cause the files are .pdf and users can download it I would like to serve the file with its original file name (that I saved in the database) instead of the cache file name.
How can I give a name to a cache file?
Or is it possible to create a cache file with a specified name?

Thank you,
Davide

Hi Davide,

Yes, you can

from TIWAppCache class, unit IWAppCache:
    // Add some existing file to the cache. Returns the URL to the file. The file name should be preferentially obtained using method NewTempFileName()
   class function AddFileToCache(aOwner: TObject; const aFileName: string; const aContentType: string; const aCacheType: TCacheType = ctOneTime): string;


You can add any existing file to the cache. It will be copied to the cache folder and some metadata will be added to it by the cache system (don't worry, only the original content will be served to the browser).

A few things you should have in mind:

1) Lifetime of the file: The lifetime time depends on CacheType parameter:
ctOneTime: after being served once, it is gone
ctForm: will live as long as the owner form
ctApplication :will live as long as the user session

if you choose ctForm, Onwer parameter must be the owner IWForm
if you choose ctApplication, Owner parameter must be the WebApplication instance or even the current active IWForm

2) Make sure you don't have 2 files with the same name added to the cache (Even from different sessions). The cache system just won't overwrite any file already in cache. If you try to add a file with duplicate name, this function will return the URL to the original file, but won't replace its contents.

3) After you call this method, just forget about that file. It does not belong to you anymore, it belongs to the cache system and cannot be changed anymore.

Let me know if you have any issues.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02