1、请求接口中,指定接收文件类型 responseType: "blob"
//例如
import request from "@/utils/requestDown"; //封装的请求文件,直接返回response.data
export function exportStatistics(data) {
return request({
url: '/perform/backstage/statistics/v1/exportStatistics',
method: 'post',
data,
responseType: "blob" //设置接收数据的类型
})
}
//下载excel的工具-目前只接文件流,调用该方法时,将接收到的返回值,直接传给改方法
export function downloadExcel(data) {
let blob = new Blob([data], {
type: "application/vnd.ms-excel;"
})
if ("download" in document.createElement("a")) {
let de = document.createElement('a');
de.style.display = "none";
let hrefLink = window.URL.createObjectURL(blob);
de.href = hrefLink; //创建下载的链接
de.download = `${new Date().getTime()}.xlsx`; //下载后文件名
document.body.appendChild(de);
de.click(); //点击下载
window.URL.revokeObjectURL(de.href); //释放掉blob对象
document.body.removeChild(de); //下载完成移除元素
} else {
console.log(blob, fileName);
}
}
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=123456.xls");
response.flushBuffer();
OutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
网友评论