【问题标题】:TransmitFile continue with page_load efter Response.EndTransmitFile 在 Response.End 之后继续 page_load
【发布时间】:2012-07-17 08:22:39
【问题描述】:

我一直在寻找一个星期的答案,我放弃了。 我正在创建一个 excelfile,然后让用户使用 TransmitFile 将其下载到他的客户端。 问题是,这在大多数情况下都能完美运行,但在某些情况下(可以重复),在 Response.End 之后,页面重新加载并调用 page_load。标志 IsPostBack 为假。 此行为会中断下载,整个应用程序停止工作,显示连接到服务器。

foreach (string filename in Directory.GetFiles(WebConfigurationManager.AppSettings.Get("EXCEL_PATH")))
{
    if (File.GetLastWriteTime(filename) < (DateTime.Now.AddDays(-1)))
    {
        File.Delete(filename);
    }
}
ClsOrderFormData ofData = new ClsOrderFormData(ddOrderTypes.SelectedValue, OF.Customer.CustomerCategory);

ClsExportOF xls = new ClsExportOF(OF.SizeDims, ofData, OF.Customer, Request.PhysicalApplicationPath, ClsPrintOrder.getOrderData().TheOrder.Articles);
string sFileName = xls.CreateExcelFile();
FileInfo OutFile = new FileInfo(WebConfigurationManager.AppSettings.Get("EXCEL_PATH") + sFileName);
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=Abacus_OF_" + ddOrderTypes.SelectedItem.Text + "_" + ddCustomer.SelectedItem.Text + "_" + DateTime.Now.ToShortDateString() + ".xls".Replace(',', '-'));
Response.ContentType = "application/vnd.ms-excel";
Response.TransmitFile(WebConfigurationManager.AppSettings.Get("EXCEL_PATH") + sFileName, 0, OutFile.Length);
Response.Flush();
Response.End();

简而言之。当它正常工作时,在 Response.End() 之后不再执行代码,正如预期的那样。当它不起作用时,它会继续 page_load 等等,并且该站点会挂起。

有什么建议吗?

【问题讨论】:

    标签: asp.net pageload transmitfile


    【解决方案1】:

    尝试使用 HttpApplication.CompleteRequest 而不是 Response.End。 Response.End 可能会抛出一个 ThreadAbortException ,这会扰乱进程。

    看... http://ardalis.com/Use-HttpApplication.CompleteRequest-Instead-of-Response.End http://weblogs.asp.net/hajan/archive/2010/09/26/why-not-to-use-httpresponse-close-and-httpresponse-end.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-29
      • 2010-12-03
      • 1970-01-01
      • 2019-07-10
      • 1970-01-01
      相关资源
      最近更新 更多