公共方法:
export const createExcel = (res, name) => {
let blob = new Blob([res], {
type: 'application/vnd.ms-excel',
});
let fileName = name + '.xlsx'; // 文件名+后缀
// 允许用户在客户端上保存文件
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, fileName);
} else {
// eslint-disable-next-line no-debugger
debugger;
var link = document.createElement('a'); //定义一个a标签
link.href = window.URL.createObjectURL(blob); //需要生成一个 URL 来实现下载,链接到blob上
link.download = fileName; //下载后的文件名称
console.log('fileName', link);
link.click(); //模拟在按钮上实现一次鼠标点击
window.URL.revokeObjectURL(link.href); //释放 URL 对象
}
};
接口调用getOrganExport方法
export const getOrganExport = (params) => http.get('/admin/organ/export', { params });
页面调用方法:
handleExportExcel() {
getOrganExport({ parentId: this.nodeId }).then((res) => {
console.log(res);
createExcel(res, 'lala');
});
},
后端返回没问题
点击导出直接报错
大佬们我哪里写错了,调不动了是在
blob的type判断一下,类似于
可能是
application/json
类型而不是你写的application/vnd.ms-excel