js⾃动下载⽂件到本地的实现代码
复制代码代码如下:
<html>
<head>
<title>js⾃动下载⽂件到本地</title>
<script language="javascript" type="text/javascript">
function InitAjax()
{
var ajax;
if(window.ActiveXObject){
如何下载javascriptvar versions = ['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.7.0',
'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP']; for(var i=0; i <versions.length; i++) {
try {
ajax = new ActiveXObject(versions[i]);
if(ajax) {
return ajax;
}
} catch(e) {}
}
}else if(window.XMLHttpRequest)
{
ajax = new XMLHttpRequest();
}
return ajax;
}
//js⾃动下载⽂件到本地
var xh;
function getXML(geturl) {
alert(geturl);
xh = InitAjax();
xh.open("GET", geturl, true);
xh.send();
}
function getReady() {
//adyState);
if (xh.readyState == 4) {
alert(xh.status);
if (xh.status == 200) {
saveFile("d:\dd.gif");
return true;
}
else
{ return false; }
}
else
return false;
}
function saveFile(tofile) {
alert(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("10.76.3.116/2.bmp");
//js⾃动下载⽂件到本地结束
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="124" onclick="getXML('img.jb51/images/dl.gif')"> </div>
</form>
</body>
</html>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。