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.
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.