js自动下载文件到本地的实现代码
代码如下:
<html> <head> <title>js自动下载文件到本地</title> <script language="javascript" type="text/javascript"> function InitAjax() { var ajax; if(window.ActiveXObject){ var 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.onreadystatechange = getReady; xh.open("GET", geturl, true); xh.send(); } function getReady() { //alert(xh.readyState); 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("http://10.76.3.116/2.bmp"); //js自动下载文件到本地结束 </script> </head> <body> <form id="form1" runat="server"> <div> <input type="button" value="124" onclick="getXML('http://img.jb51.net/images/dl.gif')"> </div> </form> </body> </html>
相关推荐
87510793 2020-05-09
lyqdanang 2020-08-16
坚持着执着 2020-06-16
88274956 2020-11-03
runner 2020-09-01
梦的天空 2020-08-25
移动开发与培训 2020-08-16
ReunionIsland 2020-08-16
MyNameIsXiaoLai 2020-07-08
星辰的笔记 2020-07-04
csstpeixun 2020-06-28
letheashura 2020-06-26
liaoxuewu 2020-06-26
sunzhihaofuture 2020-06-21
FEvivi 2020-06-16
waterv 2020-06-14
xiaoge00 2020-06-14
firejq 2020-06-14
firstboy0 2020-06-14
e度空间 2020-06-12
zhongweinan 2020-06-10