0

The user selects some options then clicks "download". At that point, my php script starts preparing the file, and it can take 5-10 minutes before the file is ready and starts downloading. I want to notify the user with a sound that the download has started.

How can I do that?

Buttle Butkus
  • 9,206
  • 13
  • 79
  • 120

1 Answers1

1

According to this question:

Is there a way to detect the start of a download in JavaScript?

There is no programmatic way to detect when a download begins. That question is six years old now, so perhaps it is out of date, but I could not find any more recent information to contradict it.

An alternative approach would be to break the download process into two parts so that you can control when the actual data transfer begins:

  1. Instead of initiating the download immediately, have the button send an AJAX request to the server asking it to prepare the file for download.
  2. The server should not reply to the AJAX immediately, but should prepare the file and save it in a temporary file storage area with a unique generated name/ID.
  3. Once the file is ready, the server should reply to the AJAX with the name/ID of the file.
  4. On the client, the AJAX completion callback can play the sound, since it knows the download is about to begin.
  5. It then uses window.open() to request the file from the server.
  6. Now the server can respond with the appropriate headers as you used to do.
  7. Finally, the server can delete the file from temporary storage (or just wait for a cron job to do it).
Community
  • 1
  • 1
radiaph
  • 4,001
  • 1
  • 16
  • 19
  • This sounds like it would work. Can you clarify how `window.open()` would get the downloaded file? Would I just create a url that points to that downloaded file and put that url into `window.open()`? Like `window.open("http://example.com/?dlfile_id=9483")`? or did you have something else in mind? – Buttle Butkus Mar 23 '15 at 05:07
  • 1
    There are various ways to do it, I'm sure, but the way you describe is how I would do it, too. – radiaph Mar 23 '15 at 05:13