axios下載二級制流形式的Excel文件。
後臺服務接口返回的Excel爲二進制流,如下圖所示:
前臺頁面利用 axios下載Excel,處理方式如下代碼所示:
//查詢條件
formInline: {
saleName: "",
doctorName: "",
goodsName: "",
startDate: "",
endDate: ""
}
axios({
method: 'post',
url: "/apr/test/export",
responseType: 'blob',
data: qs.stringify(this.formInline) //根據查詢條件,下載匹配數據的Excel表格
}).then(res => {
if (res.data) {
//獲取文件名
let fileName = res.headers["content-disposition"].substring(
res.headers["content-disposition"].indexOf("=") + 1
);
let blob = new Blob([res.data], { type: "application/x-xls" });
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, fileName);
} else {
//下載
var link = document.createElement("a");
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
link.click();
window.URL.revokeObjectURL(link.href);
}
}
});
點擊 “下載Excel“ 按鈕的效果如下圖:
完成^_^