I had to work on this kind of issue, on another project. I finally found a smart solution, as explained in another Stackoverflow question.
The explanation is given in the following post: http://gruffcode.com/2010/10/28/detecting-the-file-download-dialog-in-the-browser
The idea is to “simply” use a cookie to define when the file is downloaded.