首页 > 技术文章 > 文件流下载时 axios blob文件大小不正确?

yszzu 2019-10-22 19:43 原文

1). 文件流下载时 js blob文件大小不正确? excel乱码

res.data的字节长度length 和blob.size匹配不上。。,excel文件大小不对

axios请求里必须修改 responseType: 'blob' 'stream' 参数, 默认是json。否则axios自动使用UTF8编码response body,会破坏数据

axios官方说明 http://www.axios-js.com/docs/#Request-Config
有问题 还是看官方说明比较靠谱

另外 queryParam序列化


https://www.npmjs.com/package/qs

2). 错误处理的3种情况

https://stackoverflow.com/questions/45017822/catching-error-body-using-axios-post

1. 请求编码有问题
axios.get('wrongSetup')
.then((response) => {})
.catch((error) => {
    console.log(error.message);
})

2. 网络错误,比如跨域。 只有request, 没有reponse的情况

axios.get('network error')
.then((response) => {})
.catch((error) => {
    console.log(error.request );
});

3. 服务端响应不是2xx/3xx时  

axios.get('errorStatus')
.then((response) => {})
.catch((error) => { 
     console.log(error.response.data);  
     console.log(error.response.status);  
     console.log(error.response.headers); 
 })

推荐阅读