上传下载0117

文章描述了一段JavaScript代码,用于处理通过axios发起的POST请求下载文件的场景。根据服务器返回的数据类型,它能处理JSON错误信息或生成Excel文件(适用于IE和非IE浏览器)。下载的文件名包含了当前时间戳,并提供了对符合条件和不符合条件名单的区分。
摘要由CSDN通过智能技术生成

setExportLoading(true);
axios({
method: ‘post’,
url: ‘/approval/v1/nomination/export’, // 请求地址
data: params2, // 参数
responseType: ‘blob’, // 表明返回服务器返回的数据类型
}).then(res => {
if (res.data.type.includes(‘application/json’)) {
let reader = new FileReader();
reader.onload = (event) => {
let content = JSON.parse(reader.result);
message.error(content.msg);
}
reader.readAsText(res.data);
setExportLoading(false);
return true;
} else {
let fileName = ‘’;
if (type === ‘conformList’) {
fileName = ‘符合条件名单’ + moment().format(‘YYYYMMDDHHmmss’) + ‘.xlsx’;
} else {
fileName = ‘不符合条件名单’ + moment().format(‘YYYYMMDDHHmmss’) + ‘.xlsx’;
}
if (‘msSaveOrOpenBlob’ in navigator) {//兼容ie浏览器下载报传递给系统调用的数据区域太小.
let data = res.data;
const blob = new Blob([data], { type: ‘application/vnd.ms-excel’ });
window.navigator.msSaveOrOpenBlob(blob, fileName);
return;
} else {
const blob = res.data;
const reader = new FileReader();
reader.readAsDataURL(blob);
reader.onload = (e) => {
const a = document.createElement(‘a’);
a.download = fileName;
a.style.display = ‘none’;
a.href = e.target.result;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
}
setExportLoading(false);
}
})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值