文件流下载时 axios blob文件大小不正确?

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
有问题 还是看官方说明比较靠谱
ss

另外 queryParam序列化

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

config 中的baseUrl不一定非要是域名,可以加上uri, 比如https://www.baidu.com/abc/

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); 
 })
posted @   funny_coding  阅读(3348)  评论(1编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
build beautiful things, share happiness
点击右上角即可分享
微信分享提示