2

This question has been asked a much time, but I am unable to get a perfect answer to implement. So I give it a try asking one more time.

Suppose you wish to show a message when your file has be completely downloaded from the server. Yes, there are possible ways for uploading a file, and even showing a progress bar on download, but is there any particular instance/information/event which should tell when the download has been completed?

All I yet know is that might be it can be implemented with the help of cookies may be. But how?

Anant
  • 534
  • 11
  • 40
  • There is no way to do this. You can tell *if* a file has been downloaded [(via the use of cookies)](http://stackoverflow.com/questions/1106377/detect-when-browser-receives-file-download), but you cannot know when the file download completes. – Rory McCrossan Feb 24 '16 at 07:59
  • 1
    When I see such questions, I usually advise to download Chromium source code and implement the solution directly. But that raises the question of distributing your new browser to others... – Kevin Kopf Feb 24 '16 at 08:01
  • You can write a sort of notificator (Chrome) when download completed show a Chrome notification, and for FF and other browsers that support HTML5 notifications! – SergkeiM Feb 24 '16 at 08:05

1 Answers1

2

When you request a file from your server, send a unique key to the download service from the client.

GET /Download?fileId=someId&cookieName=abc

When the server has finished serving the file contents, set a cookie with the unique name

var cookieName = Request.QueryString["cookieName"]; 
Response.Cookies.Add(new HttpCookie(cookieName , "true")
{
    Path = "/",
    HttpOnly = false
});

Meanwhile, in your JavaScript, listen for a cookie with your unique name on it and once it is set, do a callback:

window.setInterval(function () {
    var cookie = $.cookie('abc');
        if (cookie !== null) {
            callback();
        }
}, 500);

You might also want to look into deleting the cookie when you are done.

Anders Nygaard
  • 5,433
  • 2
  • 21
  • 30
  • sounds like a nice solution. Have you tested it before? – Zim84 Feb 24 '16 at 08:38
  • We use it on a few things like file download and reports that take a while. It let's us prevent the user from clicking the download button too many times ;) – Anders Nygaard Feb 24 '16 at 08:51
  • This solution is discussed in a lot of detail here: [link](http://stackoverflow.com/questions/1106377/detect-when-browser-receives-file-download). One important issue is that the cookie is set when the request is finished. But the file download actually happens in the background (I tried this in Chrome). So if it's a really big file, your technique won't necessarily serve the purpose. There is another issue if the user click's "Cancel" on Chrome's download bar, your cookie progress trick will show it as downloaded, right? – cakidnyc Oct 26 '16 at 16:29