0

I got some exception with following code(file downloading) but it's working fine..

string filename= Server.Map Path("~/Download/");   
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filename));  
Response.WriteFile(filename);  
Response.End();

Exception is

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

So in the above code if I use HttpContext.Current.ApplicationInstance.CompleteRequest(); instead of Response.end(); the page source is appending to the file and it runs without any exception. Can anyone please clarify this? what is the cause..

NaveenBhat
  • 3,248
  • 4
  • 35
  • 48
Harish Nayak
  • 348
  • 3
  • 18
  • _"Unable to evaluate the expression because the code is optimized or a native frame is on top of the call stack"_ is a VS Watch/QuickWatch message... – Adriano Repetti May 20 '16 at 10:02
  • Instead of `Response.WriteFile` you can use `Response.TransmitFile` (which is passed off to IIS to do the work, IIRC). You shouldn't have `Response.End` in there. The code should probably be in an ashx handler rather than an aspx page. – Andrew Morton May 20 '16 at 10:04
  • Hi, Andrew If I use Response.TransmitFile, it's working fine, but why is the page source is appending to the downloaded file?? and I haven't used Response.End(now no exception)... – Harish Nayak May 20 '16 at 10:15
  • If I have any line of code after Response.end,, it will never going to execute...but my question is why is the page source is appending to the file which I have downloaded using above code(I tested for these methods Response.Transmitfile and writefile)??? – Harish Nayak May 20 '16 at 10:49
  • To avoid mixing the file and page together, you can separate out the code to send the file into an ashx handler, then use that as a URL. I assume that your `filename` variable is actually a full path to a file, not a directory as shown in your question. – Andrew Morton May 21 '16 at 14:03

3 Answers3

1

take the response out of update panel, you cant use Response.Write() during an asynchronous postback.

you have two options, delete update panel or make a trigger as PostBackTrigger in update panel

<Triggers>        
    <asp:PostBackTrigger ControlID="Button1" />
</Triggers>
Black Hole
  • 1,277
  • 12
  • 16
0

Below worked for me

Response.Flush();
Response.Clear();
HttpContext.Current.ApplicationInstance.CompleteRequest();
-1

Your answer shoule be in this link,According to This.

The Response.End method ends the page execution and shifts the execution to the Application_EndRequest event in the application's event pipeline. The line of code that follows Response.End is not executed.

Instead of Response.End(), use HttpContext.Current.ApplicationInstance.CompleteRequest()

string excelXml = GetExcelXml(dsInput, filename);
     response.Clear();
     response.AppendHeader("Content-Type", "application/vnd.ms-excel");
     response.AppendHeader("Content-disposition", "attachment;
     filename=" + filename);
     response.Write(excelXml);
     response.Flush();
     //response.End();
     HttpContext.Current.ApplicationInstance.CompleteRequest();

Also check out these for reference.

https://support.microsoft.com/en-us/kb/312629

http://weblog.west-wind.com/posts/2009/May/21/Dont-use-ResponseEnd-with-OutputCache

Is Response.End() considered harmful?

Community
  • 1
  • 1
KanisXXX
  • 757
  • 7
  • 19
  • 1
    Please give a brief instead of saying, here's a link, another link without no explanation. Link rot may occur in that case and the answer would be less than useful. – t0mm13b May 20 '16 at 10:34
  • @t0mm13b thanx for suggestion, I update the answer , Originally the answer was not mine so i tried to avoid copy paste :) – KanisXXX May 20 '16 at 10:38
  • @Vijay, I have gone through those links but not got answer for how to avoid the appending of page source to a file.. – Harish Nayak May 20 '16 at 11:04
  • @Harish did you use `Response.Flush();` before `HttpContext.Current.ApplicationInstance.CompleteRequest();` .? – KanisXXX May 20 '16 at 11:21
  • @vijay, yes I tested with Response.Flush(); same thing happening like appending page source..is there any other way to avoid the appending?? – Harish Nayak May 20 '16 at 11:45
  • @harish try `Response.Flush()` with `Response.end();` – KanisXXX May 20 '16 at 12:00
  • @vijay I tried with all the combination of above methods, but one of the thing is happening like an exception or a page source appending.. – Harish Nayak May 20 '16 at 12:40