描述
通過axios的post請求,下載excel文件
前端:
this.$http.post('/export-excel', {}, { responseType: 'blob' }).then(function (response) {
const url = window.URL.createObjectURL(new Blob([response.data]))
const link = document.createElement('a')
link.href = url
link.setAttribute('download', '導出報表.xlsx')
document.body.appendChild(link)
link.click()
})
后台:
public function exportExcel()
{
// 具體用法請參考 laravel-excel 文檔
return Excel::download(new ExcelExport(), "導出報表.xlsx");
}
現狀
本地開發導出excel亂碼,postman模擬下載正常
問題
通過開發人員工具排除xhr請求是否通過mock.js發送,當Initiator為mock.js時excel亂碼,Initiator為xhr.js時正常
刪除或注釋掉mock.js的引用,問題解決
原因
待查