[转]JavaScript加XmlHttpRequest实现文件下载

< script language = ´javascript´ >
var  xmlHttp;
try
{
  xmlHttp
=new XmlHttpRequest();
}

catch (microsoft)
{
  
try
  
{
   xmlHttp
=new ActiveXObject("Microsoft.XMLHTTP"); 
  }

  
catch(otherMicrosoft)
  
{
    
try
    
{
          xmlHttp
=new ActiveXObject("Msxml2.XMLHTTP"); 
    }

    
catch(failed)
    
{
        xmlHttp
=false;
    }

  }

}


function  getXML(geturl)
{

  xmlHttp.onreadystatechange 
= getReady; 
  xmlHttp.open(
"GET",geturl,true); 
  xmlHttp.send(); 
}



function  getReady()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200
{saveFile("d:\test.txt");return true;} 
else
{return false;}
}

else
return false;
}



function  saveFile(tofile)
{
var objStream; 
var imgs; 
imgs 
= xh.responseBody; 
objStream 
= new ActiveXObject("ADODB.Stream"); 
objStream.Type 
= 1
objStream.open();
objStream.write(imgs);
objStream.SaveToFile(tofile)
}

getXML(
" http://192.168.1.11/test.txt " );
script >

转载于:https://www.cnblogs.com/trampt/archive/2007/12/16/997024.html

你可能感兴趣的:(javascript)