[转]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>

posted @ 2007-12-16 22:20  孤城浪子  阅读(3709)  评论(2编辑  收藏  举报
博客园,让我更上一层楼