Ameba Ownd

アプリで簡単、無料ホームページ作成

Denis Benson's Ownd

Detect when browser receives download django

2021.12.19 11:19






















So it would send an empty HTML file, as well as the attached downloadable file. For example:. One possible solution uses JavaScript on the client. Disclaimer: This content is shared under creative common license cc-by-sa 3.


It is generated from StackExchange Website Network. Your email address will not be published. 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.


Then use jquery or javascript to catch the focusout event of the button to hide the container that contain the message. 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.


Then the server-side overrides this cookie with an earlier expiration age -- the completion of the server process. As soon as the cookie expiration is detected in the JS polling, "Loading Python Javascript Linux Cheat sheet Contact. Detect when browser receives file download One possible solution uses JavaScript on the client. The client algorithm: Generate a random unique token. Show the "waiting" indicator. BUT onload is not triggered in IE for file download for example, with an attachment header token.


Polling servers work, but I don't like the extra complexity. So this is my job:. Disclaimer, do not perform this operation on busy sites, as caching may increase.


However, in fact, if your site is busy for a long time, it will starve your threads anyway. Programmer Group A programming skills sharing group. Does anyone have a better idea? When the file is complete, save it under the same unique ID and If the file is ready, download it. If the file is not ready, show progress. So this is my job: Locate the hidden iFrame as usual. Generate content. Cache it with an absolute timeout in 2 minutes. Sending the javascript redirection to the callback client is actually the second call to the generator page.


Note: this will cause the onload event to be triggered in IE because it works like a regular page.