6

In our current project we are providing a PDF download that can be customized by the user through an HTML form he submits. It takes several seconds to dynamically generate the PDF and I'd like to visualize this, e.g. by disabling the submit button until the download starts. Unfortunately, I couldn't find a way to detect when the download starts*. So I wouldn't know when to re-enable the submit button.

I already tried to specify an IFrame as the target of my HTML form, hoping that the onload event would be triggered. It is not, however -- probably because the PDF is sent with a "Content-disposition: attachment" header and it is not actually loaded into the IFrame.

The only solution I can think of right now involves generating the PDF to a temporary file on the server, which I would like to avoid.

*)Let me clarify this: I wouldn't need to know if the download was finished or even if it was really started. I'd like to detect the point at which the browser will ask the user whether to open or to save the file. I guess this happens when the browser receives the HTTP header.

cg.
  • 3,648
  • 2
  • 26
  • 30
  • Do you want to know when the download starts or when it completes? Either way, I don't think that a temporary file will make a difference. On the client-side you can't tell if the file was sitting on the server or generated on the fly. – Prestaul Jan 07 '09 at 14:15
  • So you submit the form, and the response to this submission is a PDF file? During the pause, the browser is in the mode where it's waiting for a response from the server? is this correct? – Breton Jan 07 '09 at 14:16
  • @Breton: correct! @Pretaul: I'd like to know when the download starts. So, I could create a temp file, deliver some HTML response to the hidden IFrame, and then fetch the temp file by JavaScript in the onload handler. – cg. Jan 07 '09 at 15:12
  • 1
    I know you're already accepted an answer for this, but you might want to take a look at a blog post I did regarding this exact issue: http://gruffcode.com/2010/10/28/detecting-the-file-download-dialog-in-the-browser/ – Jesse Taber Feb 14 '15 at 02:38
  • @Jesse Using cookies for this is a clever idea indeed! Roughly 6 years too late for this case but it might be handy to remember. Thanks for the heads-up. – cg. Feb 16 '15 at 08:23

3 Answers3

4

If I was you I would do an AJAX call to the server with the information, generate the file, then return the file name/id/whatever back to the javascript, which then makes window.location something like download.php?id=x (but the file was already generated, so it is just setting the headers and reading it out) at which point you can re-enable the submit.

Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
2

What you want is to be able to detect when the size of the downloaded file changes from 0 to a positive value. As far as I know it is impossible to do that with javascript - you need a plug-in that can access the client's file system.

A recommended work around: Create a session per download. Have the client poll the server about the status of the download. This could be "non existed", "not started", "started", "finished". You need some server's side work to persist and update the status of the download plus an AJAX framework.

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • Thanks for your answer! I don't really care for the actual download (to a file), knowing when the HTTP response header is received would suffice. I'm not quite convinced yet, that this cannot be done in javascript. – cg. Jan 07 '09 at 15:44
  • Your suggested work around is a little more complex than what I was looking for. But I guess it would work and its surely the best idea so far. – cg. Jan 07 '09 at 15:47
  • I'm still not quite convinced that there is a better solution. But I implemented the session/status idea now, so I'll accept this answer. Thanks again. – cg. Jan 08 '09 at 11:02
  • This solutions looks best for applets because it does't require change of applet (which can be not possible sometimes) and it's very reliable (it will detect cases like 'allow java plugin' prompt in browser). But as downside, it's requires additional backend service. – setec May 28 '14 at 07:39
0

The simplest solution would be to estimate the time (generously) and do it that way. It's a hack, but it gives the desired effect. The other option might be to submit the form with a callback using Ajax to submit the form and have the generator return details to the calling page. http://www.jquery.com/ might be a good place to start for that option.

UberAlex
  • 3,477
  • 4
  • 22
  • 22