views:

200

answers:

4

ok i am downloading a file from a server and i plan to delete the file that i have downloaded on the server after it gets downloaded on the client side..

My download code is working fine but i dont know when to put the command to delete the file.

string filepath = restoredFilename.ToString();

            // Create New instance of FileInfo class to get the properties of the file being downloaded
            FileInfo myfile = new FileInfo(filepath);

            // Checking if file exists
            if (myfile.Exists)
            {

                // Clear the content of the response
                Response.ClearContent();

                // Add the file name and attachment, which will force the open/cancel/save dialog box to show, to the header
                Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);

                //Response.AddHeader("Content-Disposition", "inline; filename=" + myfile.Name);
                // Add the file size into the response header
                Response.AddHeader("Content-Length", myfile.Length.ToString());

                // Set the ContentType
                Response.ContentType = ReturnExtension(myfile.Extension.ToLower());

                //// Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
                Response.TransmitFile(myfile.FullName);

                // End the response
                Response.End();

            }

Now i know the response.End() will stop every thing and return the value, so is there another way too do so..

I need to call a function

DeleteRestoredFileForGUI(restoredFilename);

to delete the file but dont know where to put it.. i tried putting before and after Response.End() but it does not work..

any help is appreciated... thanks

+2  A: 

Add

Response.Flush();
DeleteRestoredFileForGUI(restoredFilename);

after the call to TransmitFile() and ditch the call to Response.End() (you don't need it).

If that does not work, then ditch TransmitFile() and go with:

Stream s = myFile.OpenRead();
int bytesRead = 0;
byte[] buffer = new byte[32 * 1024] //32k buffer
while((bytesRead = s.Read(buffer, 0, buffer.Length)) > 0 &&
      Response.IsClientConnected)
{
   Response.OutputStream.Write(buffer, 0, bytesRead);
   Response.Flush();
}
Bryan Batchelder
response.OutputStream.Flush() is not working...
sorry, try just Response.Flush()
Bryan Batchelder
ok the first part worked... but is there any concerns on not using response end
Response.End does nothing in this case, it's a redundant line.
o.k.w
thanks for the help...
A: 

The general pattern you are following makes me wonder, are you doing this?

  1. Create Data for Client and Save to
  2. Disk Transmit File to Client Delete
  3. File

If you are, you might change your system to work in memory. Since memory is managed in .Net you wouldn't have to do this manual cleanup, and depending on the size of the file this could be a good bit faster too:

  1. Create Data for Client and Save to MemoryStream
  2. Transmit Stream to Client
Chris
A: 

Since you set the file name in the header, you have two options:

  1. Read the file contents into a string, delete the file, echo/print the string as the body of the message.

  2. Rename the file something like delete-filename.xxx and then have some external process (maybe a cron job?) that goes behind and deletes any files beginning with that prefix.

Anthony
+1  A: 

you can't delete the file straight away as it may not have been downloaded yet. from the server side there is no easy way of telling that the file was successfully downloaded. what if an open/save dialog is opened by the browser? download won't begin until the dialog is acknowledged. (this may not be immediately and/or the dialog may be cancelled) or, what if it is a large file and the connection is dropped before it is fully downloaded? should it be possible to attempt the download again?

the normally recommended way of dealing with your situation is to do the deletion as a separate process, after a time period which allows you to be (fairly) sure the file is no longer required and/or it can be recreated/restored if need be.

depending on your situation you could have a separate process which periodically removes/processes old files. or, if you have a low volume of traffic, you could check for and delete old files each time a new one is requested.

the identification of old files will likely be based on a file time or associated value in a darabase. either way, if there are potentially lots of files to process you are unlikely to want the overhead of checking very frequently if it is unlikely to identify a lot of files to remove.
also, be sure to way up the consequences of lots of files not being removed ASAP (is disk space really an issue?) against the side effects of possibly deleting them while still needed or creating a performance side effect by checking to zealously.

Matt Lacey
so that means ill have to test it more... thanks