js 下載文件 不直接打開的方法


 1 function downloadfile(url) {
 2             var xmlHttp = null;
 3             if (window.ActiveXObject) {
 4                 // IE6, IE5 瀏覽器執行代碼
 5                 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
 6             } else if (window.XMLHttpRequest) {
 7                 // IE7+, Firefox, Chrome, Opera, Safari 瀏覽器執行代碼
 8                 xmlHttp = new XMLHttpRequest();
 9             }
10             //2.如果實例化成功,就調用open()方法:
11             if (xmlHttp != null) {
12                 xmlHttp.open("get", url, true);
13                 xmlHttp.send();
14                 xmlHttp.onreadystatechange = doResult; //設置回調函數                 
15             }
16             function doResult() {
17                 if (xmlHttp.readyState == 4) { //4表示執行完成
18                     if (xmlHttp.status == 200) { //200表示執行成功
19                         //引用js庫:http://danml.com/js/download2.js
20                         download(xmlHttp.responseText, "error.txt", "text/plain");
21                     }
22                 }
23             }
24         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM