如何避免到Response.End()"线程已被中止" Excel文件下载过程中异常 [英] How to Avoid Response.End() "Thread was being aborted" Exception during the Excel file download

查看:298
本文介绍了如何避免到Response.End()"线程已被中止" Excel文件下载过程中异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的数据集转换为Excel和下载软件,出类拔萃。我得到了我所需要的Excel file.But System.Threading.ThreadAbortException有人提出每擅长下载。
如何解决这个问题?..
请帮我...

我在ASPX screen.There也同样例外通过此方法抛出调用此方法。

我打电话,在许多ASPX屏幕和也我保持错误记录方法,其在运行时产生的异常公共无效ExportDataSet(DataSet的DS)功能权限的例外是写入到一个.txt文件。这样相同的异常是在所有的ASPX屏幕的TXT记录files.I只是想避免此异常从方法声明的类文件罚球ASPX。只是我只是想在我的方法声明class文件本身来处理这个异常。

ASPX文件的方法调用:
 excel.ExportDataSet(dsExcel);

方法定义:

 公共无效ExportDataSet(DataSet的DS)
{   尝试
   {
      字符串文件名=ExcelFile.xls;
      HTT presponse响应= HttpContext.Current.Response;
      response.Clear();
      Response.Charset的=;
      response.ContentType =应用程序/ vnd.ms-EXCEL;
      response.AddHeader(内容处置,附件;文件名= \\+文件名+\\);
      使用(StringWriter的SW =新的StringWriter())
      {
         使用(HTW的HtmlTextWriter =新的HtmlTextWriter(SW))
         {
             GridView控件DG =新的GridView();
             dg.DataSource = ds.Tables [0];
             dg.DataBind();
             dg.RenderControl(HTW);
             // RESPONSE.WRITE(样式);
             RESPONSE.WRITE(sw.ToString());
             到Response.End(); //异常在这里提出
         }
      }
   }
   赶上(异常前)
   {
      字符串ERR = ex.Message.ToString();
      EsHelper.EsADLogger(HOQCMgmt.aspx ibtnExcelAll_Click(),ex.Message.ToString());
   }
   最后
   {
   }
}


解决方案

我在网上研究,看到到Response.End()总是抛出异常。

替换此: HttpContext.Current.Response.End();

这一点:

  HttpContext.Current.Response.Flush(); //发送当前所有缓冲输出到客户端。
HttpContext.Current.Response.Sup pressContent = TRUE; //获取或设置指示是否HTTP内容发送到客户端的值。
HttpContext.Current.ApplicationInstance.CompleteRequest(); //导致ASP.NET绕过所有事件和过滤在HTTP执行管线链,并直接执行EndRequest事件。

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. How to resolve this issue ?.. Please help me...

I call this method in my aspx screen.There also same exception has thrown by this method.

I call that public void ExportDataSet(DataSet ds) function in many aspx screens and also I am maintaining error logger method for exceptions which are raised at runtime right those exceptions are write into a .txt files. So that same exception is logged in all the aspx screen's txt files.I just want to avoid this exception throws from method declared class file to aspx. Simply i just want to handle this exception at my method declaration class file itself.

ASPX File Method call: excel.ExportDataSet(dsExcel);

Method Definition:

public void ExportDataSet(DataSet ds)
{

   try
   {
      string filename = "ExcelFile.xls";
      HttpResponse response = HttpContext.Current.Response;
      response.Clear();
      response.Charset = "";
      response.ContentType = "application/vnd.ms-excel";
      response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
      using (StringWriter sw = new StringWriter())
      {
         using (HtmlTextWriter htw = new HtmlTextWriter(sw))
         {
             GridView dg = new GridView();
             dg.DataSource = ds.Tables[0];
             dg.DataBind();
             dg.RenderControl(htw);
             // response.Write(style);
             response.Write(sw.ToString());                                                
             response.End();                    // Exception was Raised at here
         }
      }
   }
   catch (Exception ex)
   {
      string Err = ex.Message.ToString();
      EsHelper.EsADLogger("HOQCMgmt.aspx ibtnExcelAll_Click()", ex.Message.ToString());
   }
   finally
   {                
   }
}

解决方案

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.

这篇关于如何避免到Response.End()"线程已被中止" Excel文件下载过程中异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆