问题描述
使用axios实现post请求方式下载文件时,需要将返回的数据类型提前定义为Bolb类型,表示其返回的是一个文件;在正常情况下后端返回到前端的是一个文件,但是当出现前端提交的参数错误或后端的程序运行异常等等情况时,后端返回给前端的数据就应该变为Json类型的数据,方便前端对错误信息等进行弹窗
解决方法
axios.post(`${BASEURL}/mobileoperator/mobileOperatorInvitation/dataCollation`,
this.map, {responseType: 'blob'}).then(response => {
if (response.data.type === "application/json") {
let fileReader = new FileReader();
fileReader.readAsText(response.data, 'utf-8')
fileReader.addEventListener("loadend", () => {
let parse = JSON.parse(fileReader.result);
let baseResponse = parse.baseResponse;
this.$Notice.error({
desc: baseResponse.message,
duration: 1
})
})
}
})