I am downloading a dynamic file in my application, simulating ajax using iframes. What I am doing is, when the download request is made, I will create a dynamic invisible iframe, and set the src of my iframe as the download url.I am able to successfully download the file, but the requirement is to show a download indicator once the download starts and that should finish as soon as the download dialog comes up.I provided call back after iframe creation to show the download indicator, which is successfully working, and provided another method on the 'onload' of the iframe, expecting it will be invoked, when the download dialog comes up. But unfortunately, that is not working, and because of that even after the download completes, my progress indicator is still there. I am not able to remove that. Then I came to realize that the since the content type of the response is not html, it will be served by a separate process, which leads to the download dialog and because of that my onload method is never getting called. Please let me know a solution for this.
相关问题
- Views base64 encoded blob in HTML with PHP
- Carriage Return (ASCII chr 13) is missing from tex
- Using :remote => true with hover event
- Is there a way to play audio on a mobile browser w
- HTML form is not sending $_POST values
You can use a flash download manager - google will reveal a slew of them.
Your other option is to use real AJAX. Simply make a request to the download URI, and when you get your response, use the responseBody instead of the responseText attribute of the XMLHTTPRequest object.
You'll need a way to write that to the harddrive, which can be done in IE with lax security constraints and the ActiveXObject "Scripting.FileSystemObject" You might be able to use the data uri approach: http://en.wikipedia.org/wiki/Data_URI_scheme with a base64 encoding, but I'm not sure if that will work.
Otherwise you're back to flash to write the data you downloaded via ajax to disk.
If you know the size of the dynamic download and exact timing isn't required in the specification, then you could do an internet speed check before the download, add some buffer time to the calculated time it will take to download the file, and just show the dialog box for that given period of time. Very hackish, but then again this scheme seems a little marketing-requires-it-to-be-so-it-shall-be
You should start a timer in the client when the download request is made which tests the state of the iframe at a specified interval:
I created the jQuery File Download plugin (Demo) which also fixes the problem and provides some other nice features. It basically gives you a "full Ajax-like" experience for file downloads (complete with callbacks even) that isn't normally possible for file downloads. It works pretty similarly to some other answers with an iframe but has some cool features that I have found quite handy:
Here is a simple use case demo using the plugin source with promises. The demo page includes many other, 'better UX' examples as well.
See this, It might help u
http://bytes.com/topic/javascript/answers/610641-file-download-ajax
See this, It might help u
http://code.msdn.microsoft.com/AjaxFileDownload