Javascript detect file download?






















This works in Firefox; it receives the empty HTML file, fires the "load" event, then shows the "Save" dialog for the downloadable file. But it fails on IE and Safari; IE fires the "load" event but doesn't download the file, and Safari downloads the file with the wrong name and content-type , and doesn't fire the "load" event. A different approach might be to make a call to start the file creation, then poll the server until it's ready, then download the already-created file.

But I'd rather avoid creating temporary files on the server. A very simple and lame one line solution is to use the window. Of course, if it takes too long and the user decides to do something else like reading emails the loading dialog will close.

I just had this exact same problem. My solution was to use temporary files since I was generating a bunch of temporary files already. The form is submitted with:. This will cause the load event on the iframe to be fired. Then the wait message is closed and the file download will then start. Tested on IE7 and Firefox. I'm very late to the party but I'll put this up here if anyone else would like to know my solution:. I had a real struggle with this exact problem but I found a viable solution using iframes I know, I know.

It's terrible but it works for a simple problem that I had. I had an html page that launched a separate php script that generated the file and then downloaded it. On the html page, i used the following jquery in the html header you'll need to include a jquery library as well :.

To break this down, jquery first launches your php script in an iframe. The iframe is loaded once the file is generated. Then jquery launches the script again with a request variable telling the script to download the file. The reason that you can't do the download and file generation all in one go is due to the php header function. If you use header , you're changing the script to something other than a web page and jquery will never recognize the download script as being 'loaded'.

I know this may not necessarily be detecting when a browser receives a file but your issue sounded similar to mine. If Xmlhttprequest with blob is not an option then you can open your file in new window and check if eny elements get populated in that window body with interval. If you have download a file, which is saved, as opposed to being in the document, there's no way to determine when the download is complete, since it is not in the scope of the current document, but a separate process in the browser.

Edit: I should not have used the word "security" in the original question, sorry for triggering the red herrings. Edit 2: My "security" phrasing misled folks into offtopic technical security issues, but both of you confirmed my suspicion that "no, there's no browser support for that.

Thanks all. It basically works by setting a cookie in the reponse header of the downloaded file, so javascript periodically can check for the existence of this cookie There's no such browser event in JavaScript and even if there was you can not trust the user's browser to provide security for you. You can then for example:. Let me clarify the last bullet. Say you're using Java - you will in. If the client disconnects you will receive an IOException during out.

On other platforms, I'm sure there are ways to tell whether the connection was lost or not. EDIT: You could actually fire a browser event using the trick outlined in the accepted answer of one of the questions you linked to. That would however not be a reliable solution to limit the number of downloads. Why is it important that the file can be downloaded "exactly once"? Once the file is downloaded it could be copied, so is there really a security issue with letting the same user download the file more than once?

If you're really worried about it, log each download request and run a scheduled report for files that were downloaded more than once. If anything looks fishy you can then examine security logs, talk to the user, etc. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Browser event when downloaded file is saved to disk Ask Question.

Asked 11 years, 9 months ago. Active 2 years, 6 months ago. Viewed 60k times. Any ideas? David Pope David Pope 6, 2 2 gold badges 33 33 silver badges 45 45 bronze badges. Would that even work? I mean the user can easily block the "oncompleted" request from being sent to the server and as such an absolutely trivial makeshift hack could harvest several copies of a given file.

Add a comment. Active Oldest Votes. Jesse Taber 2, 3 3 gold badges 22 22 silver badges 33 33 bronze badges. You can then for example: let the URL be valid only for a specific time period allow transfers only from a specific IP address associated with the unique URL let your server-side code detect when the content for a unique URL has been fully transferred and then invalidate the URL.

Then, it starts a timer, waiting for that downloadID to show up in the local, client-side cookies. That's all there is to it! If I make a request to the download page, which has a sleep command for several seconds, I get the following console output:. File still downloading How sweet is that! This is so simple, I can't believe I've never seen this before!

I'll definitely be incorporating this approach in my ColdFusion applications.



0コメント

  • 1000 / 1000