Forum OpenACS Q&A: Creation and removal of on-the-fly created download files

Hi,

i would like to implement the functionality to let users download zip
files containig xml export files and corresponding images. the user
can choose which records to export so the zip file has to be created
individually. my problem is, how to get rid of the temporary zip
files.

as i am not very experienced with aolserver i do not know when the
download script loses "focus". probably i will not be able to delete
the temp file in the same tcl file it is created ?

i am planning to create the zip file and save a note in some table
(the aolserver connection id and the corresponding filename) when the
download is started. a scheduled function could delete zip files that
have no corresponding active connection every 10 minutes.

can you think of an easier way to accomplish that ?

tia, peter

How about a cron job that deletes all files over a certain age?
An inefficient but easy way to do it is to delete the file when the connection closes. The code in your tcl page would look something like:
ns_atclose "ns_unlink -nocomplain $tmpfile"
See http://aolserver.com/docs/devel/tcl/tcl-api.adp#ns_atclose for more information on that function. Hope that helps.

An inefficient but easy way to do it is to delete the file when the connection closes

Seems to be the perfect solution to my problem, thanks a lot. Why do you think that this is inefficient ? The example in the aolserver documentation is about the removal of a temp file as well.

Sorry, I should have been more clear.

If your file creation process is time consuming, then you might want to cache the results for some period of time, say 10 minutes, and then delete the stored file.  This would expecially be true if many users might be requesting that generated file.  Even if the content is user-specific, you would have to re-generate the file on a reload.

So it is a potential trade-off.  In your case, where the content is specific to the requesting user and the zip operation is fast AFAIK, the ns_atclose method is probably the easiest thing to do.