Jump to content

A JS event when a download is complete?


jhsachs

Recommended Posts

Is there a Javascript event that's triggered when the user begins a download (by clicking the "submit" button in a Save dialog), or when a download ends?

 

Here's the background: my client has asked me to make their web site download a file on request. The sequence of events is supposed to be:

 

1. User clicks a radio button to select a "download a file" option, then a "Proceed" button.

 

2. The site initiates the download. If the user's browser is configured to open a Save dialog, it does so and starts downloading when the user closes the dialog. If not, it starts downloading immediately.

 

3. When the download finishes, the site displays a new page that says in effect, "The download is complete! These are the things you can do next..."

 

Things get complicated because of the way the download protocol works. Because browsers accept only one content-type header per request, I can't download the file, then download the "Download is complete!" page after it; the page would never display. Instead I download the "Download is complete!" page first, then it downloads the file.

 

This gets the page and the download onto the user's computer, but in the wrong order. If the download starts immediately after the page load, not much harm is done; but if the download is delayed, I leave the user staring at a page that assures her the download is complete when it hasn't even started.

 

This is liable to be a problem in many situations. The one I encountered immediately is caused by Internet Explorer's default security settings, which make IE subject the user to an lengthy and confusing interaction before letting her download the file. If the site adds to that a page which tells her that the file has already been downloaded, it will just make a difficult, confusing interaction even worse.

 

What I'd like to do is defer loading the "Download is complete!" page until the download actually is complete. That will require some kind of signal from the browser, such as an Ajax request, and I need an event to trigger that signal.

 

I looked at a list of Javascript events, but I didn't see any I could use. I'm hoping that a person more familiar with the event architecture than I am can point out something I've missed.

Link to comment
Share on other sites

Apologies I got distracted when replying to your other thread and forgot to go back to it.

 

No, there is no 'download complete' event. JavaScript's reach only extends to the DOM (Document Object Model) within HTML. Remember with what I was saying before, when you send the response as whatever content-type, you're not sending it as text/html. Therefore JS does not exist in that type of request.

 

There's also no way of wrapping the request using AJAX, as AJAX is limited to plain-text and there's very limited support for opening new windows with a specified content-type. Then again even if you did it that way, pushed the data into a new window and managed to change the content-type dynamically, the download dialogues are browser-based behaviour anyway, there's still no way of hooking onto them with JS - it's not built for that.

 

Unfortunately there's just no way of knowing (using HTML/JS) whether or not a download was successful. Instead, you should do what every other site does and change your context. Don't say "your download is complete", say "your download is starting". That will make sense and should be a familiar experience for the user.

 

Hope this helps.

Link to comment
Share on other sites

Thank you again, Adam. I thought I might get a "better" answer if I approached the problem in a different way, but I'm getting persuaded that there really is no way to do this within standard browser architecture!

 

Another possibility occurred to me -- not one I'm going to pursue now, but I wonder if it would work.

 

Suppose the download was performed through a Java applet rather than with the standard content-type header. The applet could communicate with the script in any way that met its needs, subject to tje limitations imposed by a typical configuration of Apache. When it was done, the applet could load the next "real" page.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.