如果不需要權限,直接通過鏈接下載;
需要權限,則通過ajax請求,拿到流數據
獲取數據的時候,一定要加上(responseType: ‘blob’),表示后台傳過來的數據用 blob 對象接收.
axios.post(`接口路徑`, { responseType: 'blob' });
//下載文件的方法
downfile(fileName, data) {
if ("msSaveOrOpenBlob" in navigator) {
let blob = new Blob([data]);
window.navigator.msSaveOrOpenBlob(blob, fileName);
} else {
let blob = new Blob([data], { type: "application/vnd.ms-excel" });
let fileUrl = URL.createObjectURL(blob);
let a = document.createElement("a");
a.setAttribute("href", fileUrl);
a.setAttribute("download", fileName);
a.click();
}
}
調用downfile方法下載Excel文件
(fileName: 文件名.xlsx data:后台返回的數據)
