2

I have a Webform where a user clicks a button and an Excel file is generated. This is achieved by this code:

Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=export.txt");
Response.ContentType = "text/csv";
Response.WriteFile(FILENAME);
Response.End();

I would like to add to the Response so when the user closes Excel, they can see a message on the Webform.But you can't do this in the code above.

Response.Write("Excel generated!"); ************ does not work as response will be cleared!

Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=donman_export.txt");
Response.ContentType = "text/csv";
Response.WriteFile(FILENAME);
Response.End();

How can I do this?

Petras
  • 4,686
  • 14
  • 57
  • 89

2 Answers2

1
Response.Write("Excel generated!"); ************ does not work
Response.Flush();
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=donman_export.txt");
Response.ContentType = "text/csv";
Response.WriteFile(FILENAME);
Response.End();
rick schott
  • 21,012
  • 5
  • 52
  • 81
  • That will cause this exception: Server cannot append header after HTTP headers have been sent. – Petras Oct 20 '11 at 04:26
0

Code below is doing exactly what have you asked.

It renders additional HTML tags, so download starts after message was shown to user:

    protected void Page_Load(object sender, EventArgs e)
    {
        var currentUrl   = Request.Url.OriginalString;
        var currentQuery = Request.Url.Query;

        var download = new
        {
            FilePath   = "~/test.csv",
            FileName   = "test.csv",
            FileMime   = "text/csv",

            Message    = "Excel generated!",         
            QueryParam = "direct-download",
            Delay      = 2 // seconds
        };

        var hasQueryParams = currentQuery.Length > 0;
        var isDownloadUrl  = hasQueryParams && currentQuery.IndexOf( download.QueryParam ) >= 0;

        if( isDownloadUrl ) 
        {  
            // Prepare..
            Response.ContentType = download.FileMime;
            Response.Clear();
            Response.BufferOutput = true;

            // Transfer..
            Response.AddHeader("content-disposition", "attachment; filename=" + download.FileName);
            Response.WriteFile(download.FilePath);

            // Done..
            // Instead of Response.Close()
            // http://stackoverflow.com/q/4583201/2361743
            Response.Flush();
            Context.ApplicationInstance.CompleteRequest(); 
            return;
        }

        // Meta-Refresh Tag has to be in <HEAD> section, but not all browsers follow this restriction.
        // IFRAME has to work fine. It is wrapped into <DIV> to be not visible in old browsers.
        const string tagToStartManual      = "<A href='{0}'>{1}</A>"; 
        const string tagToStartAfterDelay  = "<META HTTP-EQUIV='REFRESH' CONTENT='{1};URL={0}'>";
        const string tagToStartImmediately = "<DIV STYLE='{1}'><IFRAME SRC='{0}'></IFRAME></DIV>";
        const string cssToHideFrame        = "width:1px;height:1px;opacity:0.1;overflow:hidden";

        // Show your message..
        // And add HTML Tags which would start download:
        Response.Write(download.Message);

        var downloadUrl = currentUrl + (hasQueryParams ? "&" : "?") + download.QueryParam;

        // You don't have to use all 3 methods...
        Response.Write( String.Format( tagToStartManual, downloadUrl, download.FileName));
        Response.Write( String.Format( tagToStartAfterDelay, downloadUrl, download.Delay) ); 
        Response.Write( String.Format( tagToStartImmediately, downloadUrl, cssToHideFrame) );

        // Done. 
        // Waiting for actual download request...
    }
Vlad Mysla
  • 1,181
  • 12
  • 15