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" //設(shè)置接收數(shù)據(jù)的類型
})
}
//下載excel的工具-目前只接文件流,調(diào)用該方法時乞巧,將接收到的返回值,直接傳給改方法
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; //創(chuàng)建下載的鏈接
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();
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者