responseType为blob的请求,如何获取错误信息
exportAuditCatalog(params)
.then(res => {
let fileReader = new FileReader();
fileReader.onload = () => {
try {
let jsonData = JSON.parse(fileReader.result); // 说明是普通对象数据,后台转换失败
if(jsonData.code === 1) {
this.$message.error(jsonData.message)
}
} catch(err) {
// 解析成对象失败,说明是正常的文件流
let disposition = res.response.headers['content-disposition']
if(!disposition) {
this.$message.error('导出失败')
return
}
let filename = decodeURI(disposition.split(';')[1].split('=')[1]) // 获取文件名
if (typeof window.chrome !== 'undefined') {
// Chrome version
var link = document.createElement('a')
link.href = window.URL.createObjectURL(res.data)
link.download = filename
link.click()
} else if (typeof window.navigator.msSaveBlob !== 'undefined') {
// IE version
var blob = new Blob([res.data], {
type: 'application/force-download'
})
window.navigator.msSaveBlob(blob, filename)
} else {
// Firefox version
var file = new File([res.data], filename, {
type: 'application/force-download'
})
window.open(URL.createObjectURL(file))
}
this.multipleSelection = []
this.$refs['table'].clearSelection()
this.$message.success('导出成功!')
}
}
fileReader.readAsText(res.data);
})
.catch(err => {
this.$message.error('导出失败')
})