1

While downloading template i m getting following error message.

I have tried instead of Response.Flush(); with Response.End();. But getting same error.

Error: Excepiton in Download:System.Threading.ThreadAbortException: Thread was being aborted.
at System.Threading.Thread.AbortInternal()
at System.Threading.Thread.Abort(Object stateInfo)
at System.Web.HttpResponse.End()

Any idea to avoid above exception

Code

private void DownloadFile(string filePath, string downloadFileName)
{
    Response.ContentType = "application/ms-excel";
    Response.AddHeader("content-disposition", "attachment; filename=" + downloadFileName);
    Response.TransmitFile(filePath);
    // Response.Flush();
    Response.End();
}

Thanks in advance..

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Vinoth96
  • 353
  • 1
  • 2
  • 6
  • possible duplicate of [How to Avoid Response.End() "Thread was being aborted" Exception during the Excel file download](http://stackoverflow.com/questions/20988445/how-to-avoid-response-end-thread-was-being-aborted-exception-during-the-exce) – Luaan Jul 11 '14 at 08:35
  • Well, that's kind of what `Response.End` does. If that's not what you intended, you probably have to review the design of your application - most likely, you're doing some serious spaghetti in your request structure. – Luaan Jul 11 '14 at 08:37

1 Answers1

6

As answered here :- How to Avoid Response.End() "Thread was being aborted" Exception during the Excel file download

Replace this : HttpContext.Current.Response.End();

With this :

HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
HttpContext.Current.Response.SuppressContent = true;  // Gets or sets a value indicating whether to send HTTP content to the client.
HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline**

chain of execution and directly execute the EndRequest event.

And answered here :- ASP.NET exception "Thread was being aborted" causes method to exit

This is a ThreadAbortException; it's a special exception that is automatically rethrown at the end of every catch block, unless you call Thread.ResetAbort().

ASP .Net methods like Response.End or Response.Redirect (unless you pass false) throw this exception to end processing of the current page; your someFunctionCall() is probably calling one of those methods.

ASP .Net itself handles this exception and calls ResetAbort to continue processing.

Community
  • 1
  • 1
Neel
  • 11,625
  • 3
  • 43
  • 61