1、创建blob响应类型的axios对象
const blobAxios = axios.create({
responseType: \'blob\',
timeout: 5 * 60 * 1000,
paramsSerializer(p) {
return Qs.stringify(p, { indices: false });
}
});
blobAxios.interceptors.request.use((config) => {
const token = authToken.token;
if (token) {
config.headers.Authorization = token;
}
return config;
});
2、使用blobAxios请求文件信息
async function exportBusinessReport(url) {
const response = await blobAxios.get(url);
return downloadResponse(response);
}
3、生成a标签实现真正的下载
function downloadFile(url, filename) {
const = document.createElement(\'a\');
.href = url;
.setAttribute(\'download\', filename);
document.body.appendChild( );
.click();
document.body.removeChild( );
}
//获得文件名
function retriveFileName(response, defaultName = \'file\') {
const disposition = response.headers[\'content-disposition\'];
const matchGroup = /filename=\"?([^\"^;]+)\"?/.exec(decodeURIComponent(disposition));
if (_.isArray(matchGroup)) {
return matchGroup[1];
}
return defaultName;
}
function downloadResponse(response, exportFileName) {
const url = window.URL.create URL(new Blob([response.data]));
const filename = exportFileName || retriveFileName(response);
const notAllowedFileType = \'application/json\';
if (response.data.type !== notAllowedFileType) {
downloadFile(url, filename);
} else {
throw new StandardError({ message: \'加载异常,请联系开发查看原因\' });
}
}
继续阅读与本文标签相同的文章
上一篇 :
从零开始学习神经网络 | 视频教程
下一篇 :
spark连接hive的两种方式
-
花旗投资现金流量预测公司Cashforce,拟新添增值服务
2026-05-18栏目: 教程
-
开发者必读 · 周报 | 003期
2026-05-18栏目: 教程
-
科技巨头正在合作解决自动驾驶标准!
2026-05-18栏目: 教程
-
人工智能帮助设计自行车并打破竞速纪录
2026-05-18栏目: 教程
-
分层存储超详细解读,为什么大数据时代它已不可或缺
2026-05-18栏目: 教程
