一般下载,直接调用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) }) }