I have a popup window that displays "Please wait while your file is being downloaded". This popup also executes the code below to start the file download. How can I close the popup window once the file download has completed? I need some way to detect that the file download has completed so I can call self.close() to close this popup.
System.Web.HttpContext.Current.Response.ClearContent();
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.ContentType = fileObject.ContentType;
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", string.Concat("attachment; filename=", fileObject.FileName));
System.Web.HttpContext.Current.Response.WriteFile(fileObject.FilePath);
Response.Flush();
Response.End();
Some hacks are around that involves knowing when the last piece of the buffer has been sent or checking the
HttpResponse.IsClientConnected
property.An idea:
If you handle the file downloading yourself in server side code by writing chunk by chunk to the response stream, then you'll know when the file had finished downloading. You would simply have to connect the FileStream to the response stream, send data chunk by chunk, and redirecting after complete. This can be inside your popup window.
Make sure you check Response.IsClientConnected when writing out to the response stream.
Even though this is an old question it hasn't been answered all this time and I believe it deserves a (better) answer:
https://stackoverflow.com/a/59010319/313935
I handle the problem differently in Javascript, which might or might not work for you.
I figure once the user clicks on another element, she either already knows the download is done, or she is ready to do something else, so the message becomes irrelevant and can go away....
There is a solution where you can track the download status by transferring the file as smaller packets and check whether all the packets have been transferred. The solution is not mine but you can find it here: File Download in ASP.NET and Tracking the Status of Success/Failure of Download
The way to do that is in your pop-up to call the server via AJAX polling for some response which would indicate the file was flushed.
Ex: right before sending the file, store sessionID+FileName in a DB or session or what have you.
On the client, in your popup, poll a web-service via AJAX - this could even be a WebMethod like
Bool IsContentFlushed(string sessionID, string fileName);
After you do
Response.Flush();
remove this sessionID+FileName from your store.Call
Response.Close()
instead ofResponse.End()
- the later is very brutal, and is usually over-kill.