Detect when browser receives file download

I have a page that allows the user to download a dynamically-generated file. It takes a long time to generate, so I’d like to show a “waiting” indicator. The problem is, I can’t figure out how to detect when the browser has received the file, so I can hide the indicator.

I’m making the request in a hidden form, which POSTs to the server, and targets a hidden iframe for its results. This is so I don’t replace the entire browser window with the result. I listen for a “load” event on the iframe, in the hope that it will fire when the download is complete.

I return a “Content-Disposition: attachment” header with the file, which causes the browser to show the “Save” dialog. But the browser doesn’t fire a “load” event in the iframe.

One approach I tried is using a multi-part response. So it would send an empty HTML file, as well as the attached downloadable file. For example:

Content-type: multipart/x-mixed-replace;boundary="abcde"

--abcde
Content-type: text/html

--abcde
Content-type: application/vnd.fdf
Content-Disposition: attachment; filename=foo.fdf

file-content
--abcde

This works in Firefox; it receives the empty HTML file, fires the “load” event, then shows the “Save” dialog for the downloadable file. But it fails on IE and Safari; IE fires the “load” event but doesn’t download the file, and Safari downloads the file (with the wrong name and content-type), and doesn’t fire the “load” event.

A different approach might be to make a call to start the file creation, then poll the server until it’s ready, then download the already-created file. But I’d rather avoid creating temporary files on the server.

Does anyone have a better idea?

105 thoughts on “Detect when browser receives file download”

  1. you’re really a excellent webmaster. The website loading pace is incredible.
    It kind of feels that you’re doing any unique trick. In addition, The contents are masterpiece.
    you’ve done a fantastic task in this topic!

    Reply
  2. This design is incredible! You obviously know how to
    keep a reader amused. Between your wit and your videos, I was almost moved to start
    my own blog (well, almost…HaHa!) Wonderful job.
    I really loved what you had to say, and more than that, how
    you presented it. Too cool!

    Reply
  3. Excellent beat ! I would like to apprentice while you amend your site, how can i subscribe for a blog
    site? The account aided me a acceptable deal. I had been tiny bit familiar of this
    your broadcast provided brilliant clear idea

    Reply
  4. Great items from you, man. I have be aware your stuff previous to
    and you are just too great. I really like what you have bought
    here, certainly like what you’re saying and the way in which in which you are saying it.
    You’re making it enjoyable and you still care for to stay
    it smart. I can not wait to read much more from you.
    That is really a terrific website.

    Reply
  5. Hello! This is my 1st comment here so I just wanted to give a quick shout out and tell you I truly
    enjoy reading through your blog posts. Can you recommend any other blogs/websites/forums that deal with the same topics?
    Thanks a ton!

    Reply
  6. Hey there! This post couldn’t be written any better!
    Reading this post reminds me of my good old room mate!
    He always kept chatting about this. I will forward this page to him.
    Pretty sure he will have a good read. Thanks for sharing!

    Reply
  7. Hey there! I’ve been following your site for a while now and finally got the bravery
    to go ahead and give you a shout out from Porter Texas!
    Just wanted to tell you keep up the excellent job!

    Reply
  8. Hmm it looks like your website ate my first comment (it was super
    long) so I guess I’ll just sum it up what I submitted and say,
    I’m thoroughly enjoying your blog. I as well
    am an aspiring blog writer but I’m still new to
    everything. Do you have any suggestions for beginner blog writers?
    I’d certainly appreciate it.

    Reply
  9. First off I would like to say great blog! I had a quick question that I’d like to ask
    if you don’t mind. I was curious to find out how you
    center yourself and clear your head before writing.
    I’ve had a tough time clearing my mind in getting my ideas out there.
    I do enjoy writing but it just seems like the first 10 to 15 minutes are lost just trying to figure out how to begin. Any suggestions or hints?

    Thanks!

    Reply
  10. Great article! That is the type of info that are supposed to
    be shared around the net. Disgrace on the search engines for not positioning this put
    up upper! Come on over and seek advice from my website .

    Thanks =)

    Also visit my page … special

    Reply
  11. I’m extremely impressed with your writing skills
    as well as with the layout on your weblog. Is this a paid theme or did
    you customize it yourself? Either way keep up the nice
    quality writing, it is rare to see a nice blog like this one nowadays.

    Reply

Leave a Comment