views:

184

answers:

5

Hello,

Is there a way to tell if when a download has finished? That is, we allow users to download a report in PDF format. It takes about 3 - 5 seconds to start downloading and in this time, from click to end of download, show a wait state icon so that the user doesn't click multiple times.

Thanks! Eric

+1  A: 

You could just disable the link for 5 seconds after it is clicked, and display a hidden wait icon.

Dave Swersky
Yes, I figured I would do this but it is one of the least elegant ways, IMHO.
Eric
A: 

Can you use javascript to disable the button once it has been clicked? (http://www.codetoad.com/javascript/enable_disable_form_element.asp)

Kevin Friedheim
Yes, but how do I put it back when the download is done? Or, even put the button back once the download has started?
Eric
use a timer?setTimeout( greyOutButton(button),1250 );
Kevin Friedheim
+2  A: 

This is non-trivial. If you're using a script to send the PDF file, you could set a flag somewhere to indicate the script has completed sending the data. But this just means the script is done. It doesn't mean the bytes have been sent anywyhere. They could be stuck in the webserver's outbound queue waiting for a free slot to actually get sent out on the wire.

Best solution I've seen for this is to pop up a new window to start the download, and put the usual "please wait, I'm thinking..." animation/text in it. To keep the user from clicking the original donwload button multiple times (and popping up multiple download windows), you could put in a bit of javascript to disable the button for a short period.

Marc B
Thank you for this info. I believe you are correct in that it isn't posible.
Eric
+3  A: 

You can load the PDF in a hidden iframe with an onload handler. Here's an example:

<html>
<body>
<a href="http://sstatic.net/so/img/logo.png" target="my-frame">Download</a>
<iframe name="my-frame" src="about:blank" style="display:none"></iframe>
<p id="progress" style="display:none">Download in progress...</p>
<p id="complete" style="display:none">Download complete!</p>
<script src="http://code.jquery.com/jquery-1.4.2.min.js"&gt;&lt;/script&gt;
<script>
$(function () {
  var downloading = false;
  $("a").click(function () {
    if (downloading)
    {
        return false;
    }

    downloading = true;
    $("#progress").show();

    $("iframe").load(function () {
      downloading = false;
      $("#progress").hide();
      $("#complete").show();
    });
  });
});
</script>
</body>
</html>

I don't know of any way to detect the 'Stop' button though, so if your user stops the download it will look like it's still in progress.

Martin
Thank you Martin. This is nice but it still doesn't let me know when the download has finished or even started.
Eric
+1  A: 

Ok so this is what we did.

When a user clicks on the link to download the PDF, we show a wait state icon and make an async request to the app server to start the PDF build. When the process is done it returns the URL of the PDF. After this, we remove the wait state image and set location.href = url. I think this is the best solution.

Thanks all for the ideas/suggestions/code!

Eric