views:

910

answers:

4

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.

+1  A: 

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
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
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
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
A: 

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
A: 

I think the most suitable solution of the problem is to use some kind of progress indicator (http://en.wikipedia.org/wiki/Progress_indicator)

x-yuri
+3  A: 

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
Thanks for your answer! What you are describing is the "temporary file" solution I mentioned and that I'd like to avoid.
cg
Ooo, my apologies. Totally didn't see that part.
Paolo Bergantino