0

I've got a page that allows users to enter search criteria and then display matching records. It also has a download button to enable the user to download matching records.

How can I code it so that clicking on "Download" will first refresh the record display before downloading the data?

This is the code that I'm using for the download:

Response.ClearContent();
Response.ClearHeaders();

using (MemoryStream outputStream = new MemoryStream())
{
    // some details elided...
    outputStream.Write(documentData, 0, documentData.Count());

    string fileName = GenerateFileName();

    Response.AppendHeader("content-disposition", String.Format("attachment; filename={0}", fileName));
    outputStream.Flush();
    outputStream.WriteTo(Response.OutputStream);
}
Response.Flush();
Response.Close();
ilitirit
  • 16,016
  • 18
  • 72
  • 111

2 Answers2

1

Only one response you can send back to the browser, ether you update the data, ether you send the new header to start the download.

To make both of them you need to change your steps probably using some javascript and/or ajax call.

How HTTP protocol works: http://www.w3.org/Protocols/rfc2616/rfc2616-sec1.html

Aristos
  • 66,005
  • 16
  • 114
  • 150
0

Construct a javascript method that first updates the page via AJAX, then proceeds to make a non-AJAX request to download the file. As Aristos says, this cannot be done in a single request. A different solution could be to download the file first (non-ajax), then refresh the page without ajax. Normally, javascript code cannot be executed correctly after a new non-ajax request is made, but if it only downloads a file, I think the code might continue its execution to post the next request.

Rasmus Franke
  • 4,434
  • 8
  • 45
  • 62