1. POST方式進行文件導出;// url 下載URL // fileName 下載文件名稱 function exportFile(url, fileName) { let xhr = new XMLHttpRequest(); xhr.open("POST", url); xhr.responseType = "blob"; xhr.onload = () => { let ctx = xhr.response; let blob = new Blob([ctx]); if ("msSaveOrOpenBlob" in navigator) {//兼容IE window.navigator.msSaveOrOpenBlob(blob, fileName); } else { let aLink = document.createElement("a"); aLink.download = fileName; aLink.style.display = "none"; aLink.href = URL.createObjectURL(blob); document.body.appendChild(aLink); aLink.click(); document.body.removeChild(aLink); } }; xhr.send(); } 使用方法:exportFile(url,fileName); 使用Angular方式進行導出: .http.request("POST",url,{},{responseType:"blob"=>= ("msSaveOrOpenBlob" navigator) {= document.createElement("a"== "none"==>= = (e)=>(e && e["target"= JSON.parse(e["target"]["result"(errorMsg && errorMsg["code""有報錯,出錯了。。。。。" 2. GET方式進行文件導出;// url 下載路徑window.location = url; |
|