0

I have an ASP.NET page (using ASP.NET AJAX and the Telerik RAD components) that provides a data table and an option to download data in CSV format.

I have a requirement that a single button allow the user to download a data set. I have a system set up that's nearly working for me based off the solution in Response.Redirect to new window

My ASPX looks like this:

<asp:Button ID="ExportCsvButton" runat="server" Text="Download to CSV/Excel"
  OnClick="ExportCsvButton_Clicked" OnClientClick="aspnetForm.target = '_blank';" />

Then in my codebehind for ExportCsvButton_Clicked, I do something like this:

byte[] csvBytes = someLongOperation();
Session[EXPORTED_SESSION_KEY] = csvBytes;
Response.Redirect("myexportpage.aspx", true);

Finally, in my 'myexportpage' codebehind, I'm doing this:

protected void Page_Load(object sender, EventArgs e)
{            
  Response.Clear();
  Response.ClearHeaders();
  Response.ClearContent();

  Response.AppendHeader("content-disposition", "attachment; filename=" + "file.csv");
  Response.ContentType = "text/csv";
  Response.AddHeader("Pragma", "public");

  Response.BinaryWrite(Session[OtherPage.EXPORTED_SESSION_KEY] as byte[]);
  Response.Flush();
  Response.End();
}

This actually works quite well. When I click the 'Export to CSV' button, the page's UpdateProgress kicks in and my loading overlay appears (it's a CSS div that covers the window when visible). This can take some time depending on the data set (potentially several minutes), at which point I'm getting a window popup with my file. I can save or open the file in my browser and when I save the file I can continue on the page with my export button. Cool.

My only problem is that the UpdateProgress never goes away - the postback isn't completing (because I'm redirecting the response to the mini export page and there's no dedicated response to the postback request?).

I can use a very brittle solution, if need be, as this functionality is unlikely to be needed anywhere else in the web app for the remainder of its life.

I'm very green at AJAX so wouldn't be surprised if there isn't already an elegant solution I can use. What I really want to avoid is forcing the user to initiate a second request to initiate the download (like you see on file download sites = "your download won't begin in the next 5 seconds, so just click here").

Any thoughts?

Community
  • 1
  • 1
Stefan Mohr
  • 2,170
  • 2
  • 23
  • 35

1 Answers1

0

I don't think this is possible - the file download and the result to the UpdateProgress need two separate responses.

My solution required 2 clicks but no page transitions.

My Export button fired off the action to generate the exported data, which I serialized in the user's session. When this action completed, I hid the export button and made a 'Your download it ready. Click here to save' link visible in its place on my page. That all happened at the end of the event handler for the export button.

When the user clicked the download link, the event handler for that sent the export data to the user.

I opted to leave the download link visible after that until the user refreshed the page or changed some other controls on the page that would change what data was exportable.

Stefan Mohr
  • 2,170
  • 2
  • 23
  • 35