0

I've been looking for an answer for a week now and I give up. I'm creating an excelfile and then let the user download it to his client with TransmitFile. The thing is, this works perfectly most of the time, but in some cases (can be repeated), after the Response.End, the page reloads and page_load is called. The flag IsPostBack is false. This behaviour interrupt the download and the whole application stop working, displaying connecting to server.

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();

In short. When it works no more code is executed after Response.End(), as expected. When it don't work it continue with page_load and so on, and the site hangs.

Any tips?

1 Answers1

0

Trying using HttpApplication.CompleteRequest instead of Response.End. Response.End is probably throwing a ThreadAbortException which is messing up the process.

see... 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

Joe Web
  • 128
  • 1
  • 1
  • 7