简体   繁体   中英

How can I modify a page after a file is finished downloading to the browser?

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.

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.

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

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
   }
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM