Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I end the OutputStream during a file download?

Status
Not open for further replies.

AndyInNC

Programmer
Sep 22, 2008
76
US
I'm downloading an Excel file in ASP.Net that works in Firefox and IE, but not Chrome.

A link on a calling page opens this page in a new window which generates a spreadsheet, downloads it, then closes the window.

Code:
    protected void Page_Load(object sender, EventArgs e)
    {
        handleExport();
    }

    void handleExport()
    {
        // ...
        // Code to produce spreadsheet is here...
        // ...
        
        // Write the Excel spreadsheet from the MemoryStream and send it to the browser
        string saveAsFileName = sFileName + ".xlsx";

        Response.Clear();
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", saveAsFileName));
        workbook.Write(Response.OutputStream);
        Response.Flush();
        Response.Close();

        Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "closePage", "window.onunload = CloseWindow();");
    }

In my hex editor, the file downloaded via Chrome includes HTML markup from the aspx page which produces an error trying to open it in Excel.

Firefox and IE download it without the HTML.

I thought Response.Close() was ending the output. How do I tell Chrome that there is nothing else to download?
 
Oh yes, I have.

And different events (Page_PreRenderCompleted, Page_PreRender, etc.)

And Response.OutputStream.End()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top