封装方法
function getExel(url, params, index) {+
return new Promise(function(resolve, reject) {
let data = {
method: "GET",
url:url,
headers: {
'token': gettoken("token")
},
responseType: 'arraybuffer'
}
resolve(axios(data));
})
}
注意:responseType应设置为:'arraybuffer'
发送请求($Api已经挂载在了vue对象上,所以可以这么使用)
this.$Api.getExel("/goodsCheckService/v1/planMaterial/export?idList="+idArray)
.then(response => {
let a = document.createElement('a');
//ArrayBuffer 转为 Blob
let blob = new Blob([response.data], {type: "application/vnd.ms-excel"});
let objectUrl = URL.createObjectURL(blob);
a.setAttribute("href",objectUrl);
a.setAttribute("download", '计划单电子表格.xls');
a.click();
});
原文链接:https://blog.csdn.net/qq_37899792/article/details/90748268