接口中加上 responseType: ‘blob’,
export function downLoadTableData(data) {
return request({
url: 'xxxx',
method: 'post',
data: qs.stringify(data),
responseType: 'blob',
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
})
}
得到响应后用a标签处理
downLoadTableData({data}).then(res=>{
//创建一个a标签
const link = document.createElement('a');
//实例化一个blob出来
const blob = new Blob([res]);
link.style.display = 'none';
//将后端返回的数据通过blob转换为一个地址
link.href = URL.createObjectURL(blob);
// 设置属性和下载下来后文件的名字以及后缀名,名字和后缀名也可设置为传过来的变量
link.setAttribute(
'download',
`统计数据_${new Date().getTime()}` + `.xlsx`);
// 插入元素
document.body.appendChild(link);
//下载该文件
link.click();
//移除元素
document.body.removeChild(link);
})
以上.