views:

149

answers:

3

Hello!

I've got a simple C# ASP.NET app that generates an Excel file and sends it to the browser using the Response.WriteFile() function.

I would like to disable the submit button once it has been clicked and display a message on the screen to wait a few minutes, since some of the files can take a while to generate. This works great, but I cannot figure out how to re-activate the button, or even refresh the page, after the file is done downloading. The page stays in a disabled, "Please wait..." state even when it's finished.

I thought a synchronous AJAX call with extra javascript commands afterward would work, but although the file does get generated, it's never sent to the browser. I've also tried using an iframe control, but can't find any way to reliably know when a new file is finished loading inside it.

Thanks in advance to anyone who can help!

UPDATE: I found a possible solution at http://code.msdn.microsoft.com/AjaxFileDownload that works great for downloading files that already exist, but when I call a page that generates the file and finishes with Response.End(), the "Finished Downloading" event never fires.

+3  A: 

The way I would do this is that I'd have the page generate a token, and send said token to the server along with the request to generate the excel file.

When the server gets the token, it tosses it into a table in your database, and once the file has finished being served, it will remove the token.

Now, while all of this is happening, have a process on the page which routinely (look into JS Timers) asks the server (asynchronous calls) if the token exists in the database. when the server finally tells the page "no" then you know you can re-enable the page and hide the "Please Wait..." message.

Stephen Wrighton
That's an interesting solution, but seems like quite a lot of work and overhead just for a minor enhancement to the UI.
Chris McGrath
IMO, with the task as described, this will be the easiest solution to implement with the least number of changes.
Stephen Wrighton
A: 

I'd probably turn your code which generates and returns the file into a web service and use jquery or asp.net ajax library to make the call... with jquery it would look something like:

//open your dialog box here

then

$.ajax({
   type: "POST",
   url: "yourwebservice.asmx",
   data: "yourdata",
   success: function(){
     //close your dialog box
   }
 });
Olivieri
A: 

UPDATE: I found a possible solution at http://code.msdn.microsoft.com/AjaxFileDownload that works great for downloading files that already exist, but when I call a page that generates the file and finishes with Response.End(), the "Finished Downloading" event never fires.

It doesn't support stream downloads. You need to create a temporary file and push temp file to the browser, hope this helps in handling generated files. The end download event will be fired only when a cookie value available on the browser side.

Thanks,

Raj