js 下載檔案
阿新 • • 發佈:2022-12-07
axios({ method: 'post', // 此處不一定只是get方法,也可以通過引數傳遞 url: '/api/services/testing/testingpractice/allowAuth/exportSelectedExcel',//後臺介面 data: param,//引數 responseType: 'blob' // 此處重點:標明後端返回資料型別為流 }) .then((res) => { let blob = new Blob([res.data], {// 下載的檔案型別(此處可更改:具體取值參考以下連結地址) type: 'application/vnd.ms-excel' }); let url = window.URL.createObjectURL(blob); let link = document.createElement('a'); link.style.display = 'none'; const fileName = '學員成績模板'; // 下載時的檔名 link.download = fileName; link.href= url; document.body.appendChild(link); link.click(); }) .catch((error) => { console.log('下載檔案失敗,error' + error); });