Two methods of JS download file (GO)

Source: Internet
Author: User
Tags rar

functionDownurl (Strremoteurl, strlocalurl) {Try{            varXmlHTTP =NewActiveXObject ("Microsoft.XMLHTTP"); Xmlhttp.open ("Get", Strremoteurl,false);            Xmlhttp.send (); varAdodbstream =NewActiveXObject ("ADODB. Stream "); Adodbstream.type= 1;//1=adtypebinaryAdodbstream.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"),
However, this method has no effect on Firefox, it is available on IE browser.

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

In order to solve this problem, we can change to another way
We can write the following styles:
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" );                = URL;                = "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"        }

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.