如果不需要权限,直接通过链接下载;
需要权限,则通过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:后台返回的数据)