通过判断 axios 返回的响应类型进行判断
let res = await Axios({
url: '',
method: 'get',
responseType: 'blob',
})
if (res.type === 'application/octet-stream') {
}else {
}
通过FileReader + try/catch
let res = await Axios({
url: '',
method: 'get',
responseType: 'blob',
})
let blob = res
let fileReader = new FileReader()
fileReader.readAsText(res, 'utf-8')
fileReader.onload = (e) => {
try {
let res = JSON.parse(e.target.result)
if (res.code) {
console.log(res);
this.$message.error('导出失败')
} else {
this.downloadBlob(blob)
}
} catch (error) {
console.log(error);
this.downloadBlob(blob)
}
}
通过判断响应头Content-type
let res = await Axios({
url: '',
method: 'get',
responseType: 'blob',
})
if (response.headers['content-type'] === 'application/octet-stream') {
} else {
}
下载导出文件
const blob = new Blob([data], { type: 'application/octet-stream' });
const url = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.style.display = 'none';
link.href = url;
link.setAttribute('download', this.formExport.name + ".pdf");
document.body.appendChild(link);
link.click();
window.URL.revokeObjectURL(url);