Javascript find file i just downloaded






















I have sensitive files to download to users, and each user is permitted to download a given file exactly once. If the download fails, I want to permit the re-download, but not otherwise. The most straightforward way for this to work would be if the browser exposed a "file saved" event from the Save As But, intuition suggests there might be security holes if browsers exposed this functionality, as it sneaks somewhat outside the sandbox. I'm not sure this is even possible. I found several other questions in this area , but nothing about this problem specifically.

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. FileSystemObject , then with GetDirectory method can get a directory object. If you want to know whether the directory exists, use FolderExists.

Enumerators are an array of Directory or File objects such that you can use methods to get special file or sub directory that are in defined path.

Enumerators have some methods I have called like atEnd and moveNext in my sample. But I'd rather avoid creating temporary files on the server.

One possible solution uses JavaScript on the client. 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. Based on Elmer's example I've prepared my own solution.

After elements click with defined download class it lets to show custom message on the screen. I've used focus trigger to hide the message. After each download click you will see message your report is creating, please wait The core problem is that the web browser does not have an event that fires when page navigation is cancelled but does have an event that fires when a page completes loading.

Anything outside of a direct browser event is going to be a hack with pros and cons. Without an appropriate built-in web browser event, there aren't any perfect solutions here. However, one of the four methods above will likely be a better fit than the others depending on your use-case. Whenever possible, stream responses to the client on the fly instead of generating everything first on the server and then sending the response.

It really depends on finding a library that supports streaming content. When streaming the response as soon as the request starts, detecting the start of the download won't matter as much because it will start almost right away. Another option is to just output the download headers up front instead of waiting for all of the content to be generated first. Then generate the content and finally start sending to the client. The user's built-in downloader will patiently wait until the data starts arriving.

The downside is that the underlying network connection could timeout waiting for data to start flowing either on the client or server side. I wrote a simple JavaScript class that implements a technique similar to the one described in bulltorious answer. I hope it can be useful to someone here.

The GitHub project is called response-monitor. By default it uses spin. For more examples check the examples folder on the repository.

If you're streaming a file that you're generating dynamically, and also have a realtime server-to-client messaging library implemented, you can alert your client pretty easily. The server-to-client messaging library I like and recommend is Socket.

After your server script is done generating the file that is being streamed for download your last line in that script can emit a message to Socket. On the client, Socket. The benefit of using this method over others is that you are able to detect a "true" finish event after the streaming is done.

For example, you could show your busy indicator after a download link is clicked, stream your file, emit a message to Socket. I realize most people reading answers to this question might not have this type of a setup, but I've used this exact solution to great effect in my own projects and it works wonderfully.

My solution with a cookie: - Client side: When submitting your form, call your javascript function to hide your page and load your waiting spinner. If the cookie is found, stop checking every ms, expire the cookie and call your function to come back to your page and remove the waiting spinner removeWaitingSpinner. It is important to expire the cookie if you want to be able to download another file again! That cookie will be sent to the client when your file will be ready for download.

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.

When the user triggers the generation of the file, you could simply assign a unique ID to that "download", and send the user to a page which refreshes or checks with AJAX every few seconds. Once the file is finished, save it under that same unique ID and If you don't want to generate and store the file on the server, are you willing to store the status, e.

Your "waiting" page could poll the server to know when the file generation is complete. You wouldn't know for sure that the browser started the download but you'd have some confidence.

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. 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. BUT onload does not fire in IE for file downloads like with the attachment header token.

Polling the server works, but I dislike the extra complexity. So here is what I do:. But really, if your sites that busy the long running process will starve you of threads anyways. A quick solution if you only want to display a message or a loader gif until the download dialog is displayed is to put the message in a hidden container and when you click on the button that generate the file to be downloaded you make the container visible.

For instance, it is useful for preventing sensitive information from being sent to external servers. It includes but is not limited to multimedia objects , programs , and code snippets. Remember: as Blob has size limitations based on the supported browser, FileSaver.

See the table below to check the compatibility of FileSaver. Learn easily by using our HTML5 canvas examples. Learn Bootstrap: this Bootstrap 4 tutorial will easily teach you how to use it for your mobile web design. Learn Bootstrap quickly to become a master.

Code has been added to clipboard! Reading time 3 min. Contents 1. What are automatic downloads with JavaScript? Making JavaScript download files without the server 3.



0コメント

  • 1000 / 1000