0

我一直在寻找一个星期的答案,我放弃了。我正在创建一个 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 等等,并且该站点会挂起。

有小费吗?

4

1 回答 1

0

尝试使用 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 -使用-httpresponse-close-and-httpresponse-end.aspx

于 2012-07-18T11:05:17.970 回答