How to Avoid Response.End() “Thread was being aborted” Exception during the Excel file download

后端 未结 16 2253
感动是毒
感动是毒 2020-11-27 11:47

I tried to convert my dataset into excel and download that excel .I got my required excel file.But System.Threading.ThreadAbortException was raised every excel download. Ho

16条回答
  •  温柔的废话
    2020-11-27 12:14

    This helped me to handle Thread was being aborted exception,

    try
    {
       //Write HTTP output
        HttpContext.Current.Response.Write(Data);
    }  
    catch (Exception exc) {}
    finally {
       try 
        {
          //stop processing the script and return the current result
          HttpContext.Current.Response.End();
         } 
       catch (Exception ex) {} 
       finally {
            //Sends the response buffer
            HttpContext.Current.Response.Flush();
            // Prevents any other content from being sent to the browser
            HttpContext.Current.Response.SuppressContent = true;
            //Directs the thread to finish, bypassing additional processing
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            //Suspends the current thread
            Thread.Sleep(1);
         }
       }
    

    if you use the following the following code instead of HttpContext.Current.Response.End() , you will get Server cannot append header after HTTP headers have been sent exception.

                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.SuppressContent = True;
                HttpContext.Current.ApplicationInstance.CompleteRequest();
    

    Hope it helps

提交回复
热议问题