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