9

I have to implement GEDCOM export in my site.

My .net code created one file at server when export to gedcom clicked.

Then I need to download it to client from server, as well as user should be asked where to save that file, meaning savedialog is required.

After it's downloaded, I want to delete that file from server.

I got one code to transmit file from server to client:

Response.ContentType = "text/xml";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.TransmitFile(Server.MapPath("~/" + FileName));
Response.End();

from this LINK

but I am not able to delete the file after this code as Response.End ends response so whatever code written after that line is not execute.

If I do code to delete file before Response.End();, then file does not transmitted and I get an error.

fubo
  • 44,811
  • 17
  • 103
  • 137
Radhi
  • 6,289
  • 15
  • 47
  • 68

2 Answers2

24

Anything you put after Response.End won't get executed because it throws a ThreadAbortException to stop execution of the page at that point.

Try this instead:

string responseFile = Server.MapPath("~/" + FileName);

try{
    Response.ContentType = "text/xml";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
    Response.TransmitFile(responseFile);
    Response.Flush();
}
finally {
    File.Delete(responseFile);
}
Josh
  • 68,005
  • 14
  • 144
  • 156
  • 1
    What Response.Flush does and what Response.End does?? can please let me know difference? – Radhi Apr 22 '10 at 05:24
  • Response.Flush forces out any buffered output (if there is any) but does not throw the ThreadAbortException - the response is still in progress. Response.End flushes but then throws a ThreadAbortException which cannot be stopped. Putting the delete code in the Finally block ensures that it will run no matter what the outcome. – Josh Apr 22 '10 at 05:30
  • 6
    This does not handle the situation when the user clicks Cancel in the file download dialog. When that happens an HttpException is thrown with the message "The remote host closed the connection. The error code is 0x800703E3." then in the finally block the Delete fails with an IOException - "The process cannot access the file 'C:\Windows\TEMP\tmp5CA3.tmp' because it is being used by another process." I added a catch (HttpException) and called Response.End() inside the catch and that worked for me – Colin Feb 27 '13 at 10:21
2

If the file is reasonably small, you can load it into a byte array so that you can delete the file while still being able to send the data:

Response.ContentType = "text/xml";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
string path = Server.MapPath("~/" + FileName);
byte[] data = File.ReadAllBytes(path);
File.Delete(path);
Response.BinaryWrite(data);
Response.End();
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Hi, does Response.TransmitFile and Response.BinaryWrite have any difference in performace? – Radhi Apr 22 '10 at 05:23
  • @Radhi: Not really. BinaryWrite is faster of course as you already have the data in memory, but together with loading the data it does the same thing as TransmitFile. – Guffa Apr 22 '10 at 06:26