下载文件流,需要接口设置responseType:'blob',接口设置了responseType:'blob'后,拿不到后端接口的异常信息,我们只需要添加如下代码:
const service = axios.create({
baseURL: ***, // url = base url + request url
// withCredentials: true, // send cookies when cross-domain requests
timeout: 30000 // request timeout
})
// 请求拦截器
service.interceptors.response.use(
response => {
// 省略。。。
},
error => {
// console.log('err' ,JSON.parse(JSON.stringify(error))) // for debug
// 关键!!! 拿到blob信息
if(error.response&&error.response.data instanceof Blob&&error.response.data.type==='application/json'){
error.response.data.text().then(text=>{
const json=JSON.parse(text)
Message({
message: json.message,
type: 'warning',
duration: 5 * 1000
})
})
return Promise.reject(json.message)
}
// 普通异常
Message({
message: JSON.parse(JSON.stringify(error.response)).data.message,
type: 'error',
duration: 5 * 1000
})
return Promise.reject(JSON.parse(JSON.stringify(error.response)).data.message)
}
)