以下是我所做的
在Laravel
return Excel::download($report, 'coupons-report.xlsx');
在Vue
async submitDates() {
await this.$axios({
url: "/exports/coupons?from=01-01-2020&to=30-03-2022", //your url
method: "GET",
responseType: "blob"
}).then(response => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement("a");
link.href = url;
link.setAttribute("download", "file");
document.body.appendChild(link);
link.click();
}); // Please catch me!
},
我的期望
- 下载了包含预期数据的Excel文件
实际发生了什么
- 具有null的文件
注意:当我使用Postman并将响应保存为文件时,我会得到正确的结果。
我不确定这是否会对你有所帮助,但这就是我使用Laravel Excel从VueJS组件下载文件所做的:
window.open("/exports/coupons?from=01-01-2020&to=30-03-2022").focus();
这将打开一个新窗口,下载文件,然后关闭。
Excel::download($report, 'coupons-report.xlsx')
似乎没有将blob作为返回。您可以尝试使用此responseType: 'arraybuffer'
作为响应,并将const url
设置为
var url = new Blob([response], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});