21

I'm writing a simple test page to download a text file from a browser on button click. I am getting a really strange error that I have never seen before. Any thoughts?

The error occurs on Response.End(); and the file never gets to the client browser

Code:

  string filePath = "C:\\test.txt";
  FileInfo file = new FileInfo(filePath);
  if (file.Exists)
  {
    Response.ClearContent();
    Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
    Response.AddHeader("Content-Length", file.Length.ToString());
    Response.ContentType = "text/plain";
    Response.TransmitFile(file.FullName);
    Response.End();
  }

Error:

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.

tier1
  • 5,797
  • 6
  • 41
  • 69
  • 1
    remove the end, and try the Responce.Flush() and the Responce.ApplicationInstance.CompleteRequest(); – Aristos Jan 17 '12 at 15:46
  • Good call, that stopped the exception from being thrown. But the code still completes without anything happening on the browser. ?? – tier1 Jan 17 '12 at 15:55

2 Answers2

39

Try changing it to.

 Response.Clear();
 Response.ClearHeaders();
 Response.ClearContent();
 Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
 Response.AddHeader("Content-Length", file.Length.ToString());
 Response.ContentType = "text/plain";
 Response.Flush();
 Response.TransmitFile(file.FullName);
 Response.End();
RousseauAlexandre
  • 1,484
  • 19
  • 24
Ashwin Chandran
  • 1,417
  • 13
  • 11
  • Same as before, runs through the code and throws an exception on Response.End(). It seems weird that Response.TransmitFile executes ok but the file does not get transfered? – tier1 Jan 17 '12 at 16:10
  • 1
    @Ashwin: I'm trying this solution in my code-behind file (my issue is http://stackoverflow.com/questions/18599735/downloading-a-dynamic-file), but no matter where I attempt to call `TransmitFile()` or `WriteFile()` I keep getting an exception: **Server cannot set content type after HTTP headers have been sent.** Any suggestions? – Codes with Hammer Sep 04 '13 at 20:34
  • Response.End() throws an exception by nature. – yazanpro May 07 '14 at 15:24
  • This worked for our application which uses this method to allow users to download .svg files. – ahwm Jan 26 '16 at 00:28
  • 1
    `Response.Clear();` does the [same as](http://stackoverflow.com/a/9044607/641833) `Response.ClearContent();` Why have both? – Trisped Jul 15 '16 at 21:40
  • Can this be done with with a filestream? The user chooses the save location and then we stream the file to that location? – bgmCoder Jun 06 '17 at 18:08
  • how to use this in a asp webapi? – Andreas Dec 20 '17 at 12:56
8

Just a slight addition to the above solution if you are having problem with downloaded file's name...

Response.AddHeader("Content-Disposition", "attachment; filename=\"" + file.Name + "\"");

This will return the exact file name even if it contains spaces or other characters.

E Malik
  • 2,237
  • 1
  • 21
  • 12