This code work fine on localhost but on my server when i click the download button it open the complete file as text in the browser – user3316523 May 1 '14 at 13:15 It really depends on which backend you are using to server the file.
I have on html page input type text. I want paste URL, on example http://www.quicksprout.com/images/foggygoldengatebridge.jpg in this text field and click button 'download' and download this file to my computer. I want want to realize this with AJAX. Maybe do you know some AJAX plugins or code that could realize downloading file from HTTP URL?
2 Answers
You don't need AJAX to do that, nor would you be able to because of CORS limitations. Instead try something like this.
HTML:
JavaScript:
Not the answer you're looking for? Browse other questions tagged javascriptjqueryhtmlajax or ask your own question.
jQuery File Download is a cross server platform compatible jQuery plugin that allows for an Ajax-like file download experience that isn't normally possible using the web.
Demo (of this exact source):
Source here:
Targeting pretty modern browser(s)?
This might be a better solution to your problem https://github.com/eligrey/FileSaver.js/
Jquery Download File Using Ajax
Requirements
- jQuery 1.6+
Common questions and answers
- Q: I need to send in custom headers. How do I do that?
- Unfortunately since this plugin uses an iframe and not AJAX you cannot send in custom headers. If you really need to do this and are willing to accept a more narrow range of browser support this might be a better solution to your problem https://github.com/eligrey/FileSaver.js/
- Q: It doesn't work!?
- Try out the demo and make sure you are doing exactly what it is doing: http://jqueryfiledownload.apphb.com/ If the demo doesn't work in your browser you have defintely found a bug and us know!
- Due to iframe security restrictions you must serve up the file from the same domain you see in the address bar
Supported and tested browsers
Jquery Download File From Url To Computer
- Internet Explorer 6+ - Works fine for standard use cases except in < IE9 JavaScript access to the failed response HTML doesn't (and can't) work reliably due to browser iframe constraints.
- Firefox 11+ - reasonably sure it will work on earlier versions
- Chrome 17+ - reasonably sure it will work on earlier versions
- iOS 5.0+ - reasonably sure it will work on earlier versions
- Android 4.0+ - non-GET requests do not work due to a long-standing bug in the Android browser. This is handled 'gracefully' with a message to the user.
Example of writing cookie (MVC):
https://github.com/johnculviner/jquery.fileDownload/blob/master/src/Common/FileDownloadAttribute.cs
The above attribute can be used on any controller action that may return a file download that you would like to use jquery.fileDownload.js with
Jquery Download File From Url Free
For more information and documentation please visit:
Or look at the well documented JavaScript source:
Ruby on Rails integration
The jquery_file_download-rails
gem integrates jquery.fileDownload.js
into the Rails 3.1+ asset pipeline.