通常我们下载文件,后端会返回文件流或者返回一个文件地址。这时我们就需要对返回的进行处理。
一、文件流返回
如下是返回的文件流
代码如下
// 下载
async exportList() {
const params = window.utilsMap.getListParams(this);
const res = await window.apisMap.carManage.carExport({ ...params, ...this.page });
this.downloadByStream(res.data, '车辆管理列表.xlsx');
}
// 下载流
downloadByStream (stream, filename) {
const blob = new Blob([stream]);
const eLink = document.createElement('a');
eLink.download = filename;
eLink.style.display = 'none';
eLink.href = URL.createObjectURL(blob);
document.body.appendChild(eLink);
eLink.click();
URL.revokeObjectURL(eLink.href);
document.body.removeChild(eLink);
},
二、地址返回
接口返回一个地址的话,就更容易了,代码如下
// 定义接口
// 导出excel采购单排产处理记录
productionExport: params => Instances.EISENHOWER({
url: '/purchaseOrder/scheduling/production/export',
method: 'get',
params,
responseType: 'blob',
})
// 下载明细
async productionExport(row) {
const { data } = await window.apisMap.purchaseOrder.productionExport({ batchNo:row.batchNo });
window.location = data;
}