JS download file to local (multi-browser compatible)

Source: Internet
Author: User

In the client through the JS download file, tried several download methods, IFRAME mode is limited to IE browser, window.open (URL), Location.href=url These two ways in the Chrome browser will also be directly open the file rather than download, Baidu N Long no results, in Google or find the answer, download links here.

Window.downloadfile =function(sURL) {//IOS devices do not support downloading. We have the to inform user on this.    if(/(IP)/g.test (navigator.useragent)) {Alert (' Your device does not the support files downloading. Try again in desktop browser. '); return false; }    //If in Chrome or safari-download via virtual link click    if(Window.downloadFile.isChrome | |Window.downloadFile.isSafari) {//Creating New link node.        varlink = document.createelement (' A '); Link.href=sURL; if(Link.download!==undefined) {            //Set HTML5 Download attribute. This would prevent file from opening if supported.            varFileName = surl.substring (Surl.lastindexof ('/') + 1, surl.length); Link.download=FileName; }        //dispatching Click event.        if(document.createevent) {varE = document.createevent (' mouseevents '); E.initevent (' Click ',true,true);            Link.dispatchevent (e); return true; }    }    //Force File Download (whether supported by server).    if(Surl.indexof ('? ') = = =-1) {sURL+ = '? Download '; } window.open (sURL,' _self '); return true;} Window.downloadFile.isChrome= Navigator.userAgent.toLowerCase (). IndexOf (' Chrome ') >-1; Window.downloadFile.isSafari= Navigator.userAgent.toLowerCase (). IndexOf (' Safari ') >-1;

JS download file to local (multi-browser compatible)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.