views:

16

answers:

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 http://stackoverflow.com/questions/104601/asp-net-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?