Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make code execute after Response.end

Tags:

c#

asp.net

My code is like this

HttpContext.Current.Response.Clear();
     HttpContext.Current.Response.ContentType = "application/pdf";
     HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + "name" + ".pdf");
     HttpContext.Current.Response.TransmitFile("~/media/pdf/name.pdf");
     HttpContext.Current.Response.End();
     if (FileExists("/media/pdf/name.pdf"))
     {
         System.IO.File.Delete("D:/Projects/09-05-2013/httpdocs/media/pdf/name.pdf");
     }

Here I want to download name.pdf in the browser, and after the download I want o delete that file.But the code execution stops at

HttpContext.Current.Response.End();

no code after that line is executed.so my delete function is not working.Is there any work around for this issue?

like image 490
None Avatar asked Sep 25 '26 11:09

None


1 Answers

HttpResponse.End (as per documentation) raises a ThreadAbortException and as you do no attempt to handle this your method exits.

I'm not sure exactly why you must use End(), but you could put the "cleanup" code in a finally statement.

like image 155
Marcus Avatar answered Sep 27 '26 02:09

Marcus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!