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

后端 未结 16 2127
感动是毒
感动是毒 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:04

    For me only works

    HttpContext.Current.ApplicationInstance.CompleteRequest().

    https://stackoverflow.com/a/21043051/1828356

    0 讨论(0)
  • 2020-11-27 12:06

    I researched online and saw that the Response.End() always throws an exception.

    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.
    
    0 讨论(0)
  • 2020-11-27 12:07

    Use a special catch block for the exception of the Response.End() method

    {
        ...
        context.Response.End(); //always throws an exception
    
    }
    catch (ThreadAbortException e)
    {
        //this is special for the Response.end exception
    }
    catch (Exception e)
    {
         context.Response.ContentType = "text/plain";
         context.Response.Write(e.Message);
    }
    

    Or just remove the Response.End() if your building a filehandler

    0 讨论(0)
  • 2020-11-27 12:08

    the error for Response.END(); is because you are using a asp update panel or any control that using javascript, try to use control native from asp or html without javascript or scriptmanager or scripting and try again

    0 讨论(0)
  • 2020-11-27 12:10

    Just put the

    Response.End();
    

    within a finally block instead of within the try block.

    This has worked for me!!!.

    I had the following problematic (with the Exception) code structure

    ...
    Response.Clear();
    ...
    ...
    try{
     if (something){
       Reponse.Write(...);
       Response.End();
    
       return;
    
     } 
    
     some_more_code...
    
     Reponse.Write(...);
     Response.End();
    
    }
    catch(Exception){
    }
    finally{}
    

    and it throws the exception. I suspect the Exception is thrown where there is code / work to execute after response.End(); . In my case the extra code was just the return itself.

    When I just moved the response.End(); to the finally block (and left the return in its place - which causes skipping the rest of code in the try block and jumping to the finally block (not just exiting the containing function) ) the Exception ceased to take place.

    The following works OK:

    ...
    Response.Clear();
    ...
    ...
    try{
     if (something){
       Reponse.Write(...);
    
       return;
    
     } 
    
     some_more_code...
    
     Reponse.Write(...);
    
    }
    catch(Exception){
    }
    finally{
        Response.End();
    }
    
    0 讨论(0)
  • 2020-11-27 12:11

    I used all above changes but still I was getting same issue on my web application.

    Then I contacted my hosting provide & asked them to check if any software or antivirus blocking our files to transfer via HTTP. or ISP/network is not allowing file to transfer.

    They checked server settings & bypass the "Data Center Shared Firewall" for my server & now our application is able to download the file.

    Hope this answer will help someone.This is what worked for me

    0 讨论(0)
提交回复
热议问题