<div class="codetitle"> <span><a style="CURSOR: pointer" data="56493" class="copybut" id="copybut56493" onclick="doCopy('code56493')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code56493"> <br> <br> <br><title>js自动下载文件到本地</title> <br><script language="javascript" type="text/javascript"> <BR>function InitAjax() <BR>{ <BR>var ajax; <BR>if(window.ActiveXObject){ <BR>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']; <BR>for(var i=0; i <versions.length; i++) { <BR>try { <BR>ajax = new ActiveXObject(versions[i]); <BR>if(ajax) { <BR>return ajax; <BR>} <BR>} catch(e) {} <br><br>} <BR>}else if(window.XMLHttpRequest) <BR>{ <BR>ajax = new XMLHttpRequest(); <BR>} <br><br>return ajax; <BR>} <BR>//js自动下载文件到本地 <BR>var xh; <BR>function getXML(geturl) { <BR>alert(geturl); <BR>xh = InitAjax(); <BR>xh.onreadystatechange = getReady; <BR>xh.open("GET", geturl, true); <BR>xh.send(); <BR>} <br><br>function getReady() { <br><br>//alert(xh.readyState); <BR>if (xh.readyState == 4) { <BR>alert(xh.status); <BR>if (xh.status == 200) { <BR>saveFile("d:\dd.gif"); <BR>return true; <BR>} <BR>else <BR>{ return false; } <BR>} <BR>else <BR>return false; <BR>} <br><br>function saveFile(tofile) { <BR>alert(tofile); <BR>var objStream; <BR>var imgs; <BR>imgs = xh.responseBody; <BR>objStream = new ActiveXObject("ADODB.Stream"); <BR>objStream.Type = 1; <BR>objStream.open(); <BR>objStream.write(imgs); <BR>objStream.SaveToFile(tofile) <BR>} <BR>//getXML("http://10.76.3.116/2.bmp"); <BR>//js自动下载文件到本地结束 <BR></script> <br><br> <br> <br><form id="form1" runat="server"> <br><div> <br><input type="button" value="124" onclick="getXML('http://img.jb51.net/images/dl.gif')"> <br> </div> <br> </form> <br> <br><br> </div>