JS Download file

Source: Internet
Author: User

function Downurl (Strremoteurl, Strlocalurl) {        try{            var xmlHTTP = new ActiveXObject ("Microsoft.XMLHTTP");            Xmlhttp.open ("Get", Strremoteurl, false);            Xmlhttp.send ();            var adodbstream = new ActiveXObject ("ADODB. Stream ");            Adodbstream.type = 1;//1=adtypebinary             adodbstream.open ();            Adodbstream.write (xmlhttp.responsebody);            Adodbstream.savetofile (Strlocalurl, 2);            Adodbstream.close ();            Adodbstream = null;            XmlHTTP = null;        }        catch (e) {            window.confirm ("Error downloading URL!");        }        Window.confirm ("Download done.");     

For example, the address to download the file is: htpp://www.baidu.com/ test.rar  
We can use window.open ("Htpp://www.baidu.com/test.rar"),   
But this method has no effect on Firefox and is available on IE.   

If you use window.location.href= "Htpp://www.baidu.com/test.rar", some versions of Firefox are not supported.   


We can write the following style:   
Window.location= "Htpp://www.baidu.com/test.rar";  

2. Download the file to local (default) (IE and Firefox are available)

function DownloadFile (URL) {           try{             var elemif = document.createelement ("iframe");               elemif.src = URL;               ElemIF.style.display = "None";               Document.body.appendChild (ELEMIF);           } catch (E) {          }     }

JS Trigger:
<div class= "Login_center" ><a href= "Javascript:_login ()" ></a></div>
<input type= "button" value= "into 1" OnClick = "downloadFile (' Http://wan.exe ')" >

Note: In HTML, the properties of the IFRAME are SRC, but in JS, only some browsers support the modification of SRC (read is no problem), the real common is to modify the corresponding frame of the href value.

function Switchiframe () {    window.frames["framename"].location.href= "action (or the name you need). aspx"        }

JS Download file

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.