<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.XMLHT .5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP']; <BR>for(var i=0; i <versions.length; i ) { <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><BR>return ajax; <BR>} <BR>/js自動下載檔案到本機<BR>var xh; <BR>function getXML(geturl) { <BR>alert(geturl); <BR>xh = InitAjax(); <br>xh.onreadystatechangechange = getReady; <br>x.openopenchange ("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> { <BR>alert(tofile); <BR>var objStream; <BR>var imgs; <BR>imgs = xh.responseBody; <BR>objStream = new ActiveXObject("ADODB.Stream"); <Stream🎜>objStream = new ActiveXObject("ADODB.Stream"); <Stream🎜>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> </div>