请求xlsx文件后,返回错误,解析接口返回错误信息
重点
if (data.type === 'application/json') {
const fileReader = new FileReader();
fileReader.readAsText(data, 'utf-8');
fileReader.onload = () => {
// 按照接口返回规范调整
const err = JSON.parse(fileReader.result).message; // FI002
return message.error(err);
};
} else {
正常实现
}
const handleDownload = row => {
if (row.exportUrl) {
listQuery.loading = true;
downloadFileApi(row.exportUrl)
.then(res => {
const { data } = res;
if (data.type === 'application/json') {
const fileReader = new FileReader();
fileReader.readAsText(data, 'utf-8');
fileReader.onload = () => {
const err = JSON.parse(fileReader.result).message; // FI002
return message.error(err);
};
} else {
const filename = decodeURIComponent(
res.headers['content-disposition']
.split('filename=')[1]
.split('.')[0]
);
exportFile(
new Blob([data], {
type: 'application/vnd.ms-excel;charset=utf-8'
}),
filename,
res.headers['content-disposition'].split('.')[1]
);
}
})
.catch(err => {
message.error(err);
})
.finally(() => {
listQuery.loading = false;
});
}
};