Thread was being aborted Error ? In asp.net?

2020-03-30 06:49发布

问题:

In web application, i am using code, to download the documnet which is uploaded, which is written in item command event of datalist, but it s giving error like "Thread was being aborted" can you help me to solve this, thank you. My code is as fallows.

protected void dtlstMagazine_ItemCommand(object source, DataListCommandEventArgs e)
{
   try 
   { 
    objItMagBe.TitleId = Convert.ToInt32(e.CommandArgument);
    dts = new DataSet();
    dts = objItMagBL.GetMagzineByTitleID(objItMagBe);
    GetPopularMagazine();

    string Myfile = "../xxxx/DataFiles/" + dts.Tables[0].Rows[0]["Files"].ToString();
    if (File.Exists(Server.MapPath(Myfile)) == true)
    {
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + Myfile);
        Response.TransmitFile(Server.MapPath(Myfile));
        Response.End();
        Response.Flush();
    }
   }
   catch
   {

    }
}

回答1:

Your Response.End() is most likely what is causing that. It is because it is telling ASP.NET that the request is over.

What is the stack trace from the Exception, where does .NET think the exception is being thrown from?



回答2:

It seems that the file is big so the IIS is killing the thread, you should change this in the web.config:

<httpRuntime 
    maxRequestLength="1048576"
    executionTimeout="3600"
  />

maxRequestLength is in bytes

executionTimeout is in seconds



回答3:

You sure that doesn't happen in other code not shown ? usually you get that with when you redirect while inside a try-catch



回答4:

The issue is that you're calling Response.End() before Response.Flush(). Swap the order of those statements and the issue will go away.



标签: asp.net