轉自:https://blog.csdn.net/Angel_jn/article/details/108059927 (前端 XMLHttpRequest 實現下載excel文件)
const xhr = new XMLHttpRequest(); xhr.open('get', 'http://192.168.1.102:3333/'); xhr.send(); xhr.responseType = 'blob'; //設置請求回來的數據為blob方式 xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { // 數據在 this.response 保存 // excel 的 MIME 格式為 application/vnd.ms-excel var blob = new Blob([this.response], { type: "application/vnd.ms-excel" }); // 創建a鏈接 href鏈接地址 download為下載下來后文件的名稱 var aa = document.createElement('a'); aa.href = URL.createObjectURL(blob); aa.innerHTML = 'a鏈接'; aa.download = 'aa.xls'; aa.style.display = 'none'; //隱藏a標簽 直接調用a標簽的點擊事件 document.body.appendChild(aa); aa.click(); } }