一般下載,直接調用ifream方式即可。
但是有些下載方式,需要添加請求頭,ifream加不了請求頭,因此可以參考以下寫法:
// 文件下載 && 文件導出 function exportData(obj) { let fileType = '' if (obj.fileType.indexOf('zip') != -1) { fileType = 'application/zip' } else if (obj.fileType.indexOf('xlsx') != -1) { fileType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' } else if (obj.fileType.indexOf('xls') != -1) { fileType = 'application/vnd.ms-excel' } else if (obj.fileType.indexOf('pdf') != -1) { fileType = 'application/pdf' } axios({ url: obj.url, method: obj.method, data: obj.data || {}, responseType: 'blob', xsrfHeaderName: 'Authorization', headers: { 'Content-Type': 'application/json', 'Authorization': getToken() }, }).then(response => { let blob = new Blob([response.data], { type: fileType }) if (obj.fileType === 'pdf') { window.open(window.URL.createObjectURL(blob)) return } let url = window.URL.createObjectURL(blob) let link = document.createElement('a') link.style.display = 'none' link.href = url console.log('response',response,url) link.setAttribute('download', obj.fileName) document.body.appendChild(link) link.click() }).catch(error => { console.log(error) }) }