tags:

views:

613

answers:

3

As part of a Classic ASP Project the user should be able to download a file - which is dynamicly extracted from a zip archive and sent via Response.BinaryWrite() - by simply calling "document.asp?id=[some id here]".

Extracting and sending is not the problem but I need to delete the extracted file after the download finished. I never did any ASP or VBA before and I guess that's why I stuck here.

I tried deleting the file right after Response.WriteBinary() using FileSystemObject.DeleteFile() but this results in a 404-Error on the client-side.

How can I wait till the download finished and then do additional actions?

Edit: This is how my code looks like:

'Unzip a specified file from an archive and put it's path in *document*

set stream = Server.CreateObject("ADODB.Stream")
stream.Open
stream.Type = 1 ' binary
stream.LoadFromFile(document)

Response.BinaryWrite(stream.Read)

'Here I want to delete the *document*
A: 

Is it not possible to stream the file into memory, then binary write the stream to the browser, this way the file is never created on the server and there is no need to delete it.

Mark Redman
As I said, the file gets unzipped from an archive and I don't think it's possible to extract it right into a stream.
J. Random Coder
Sure, I guess it may depend on the component you're using. In that case if you have to save to disk eg to a "temp" folder, you could build a windows service that cleans up files of a specific age?
Mark Redman
That's not an option for me. The ASP-Website is just a (really) small part of a big project and I can't expect the customer to install a windows service for a feature he'll probably never use.
J. Random Coder
If its rarely used feature, just let the temp folder fill up and delete the temp files periodically through a normal maintenance procedure.
Mark Redman
It would be possible, but it's not the nice way. When - for some reasons - a customer uses this feature often, the number of documents in the temp folder would grow rapidly. I found a different way to manage my problem, see my answer.
J. Random Coder
You are correct. I was just reacting to you previous statement " feature he'll probably never use".
Mark Redman
A: 

I suspect that the point you are calling the DeleteFile method the file you are trying delete is currently locked by something else, the question is what?

Try including:-

 stream.Close()

after your BinaryWrite. Also make sure you've done a similar thing to the component you've used to extract the file. If the component doesn't offer any obviouse "close" methods they trying assigning Nothing to the variables referencing them.

AnthonyWJones
I tried stream.Close(), but it had no effect. I got the same 404-error as before and I'm sure it has nothing to do with the zip component, because it's a C# program which gets called via WScript.Shell.Run().
J. Random Coder
A: 

I found a solution: The extracted files are saved in a special directory and everytime a user runs the document.asp it checks this directory for files older than one hour and deletes them.

I think it's the simplest way to manage, but furthermore I would prefer a solution where the document is deleted after downloading.

J. Random Coder