图片显示(后端返回接口在预览中展示图片,响应无返回)后端返回二进制图片前端乱码解决方案
https://blog.csdn.net/weixin_46801282/article/details/123386264
// base64数据
this.imgSrc = 'data:image/jpg;base64,' + res.payload.imageStr;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 解决方案一:<br>后端把图片转码成base64再发送过来router.get( '/test' , (req, res) => { fs.readFile( './test.png' , 'binary' , function (err, file) { if (err) { console.log(err) return } else { res.send({ status: 0, message: '调取成功' , data: Buffer.from(file, 'binary' ).toString( 'base64' ), }) } }) }) |
1 2 3 4 5 6 7 8 9 10 11 | 解决方案二 添加 {responseType: ‘arraybuffer’} 请求头,获取ArrayBuffer类型数据,再转码成base64 axios.get( 'http://localhost:801/my/test' , { responseType: 'arraybuffer' }).then(res => { console.log(res) // buffer转译 console.log(btoa( new Uint8Array(res.data).reduce((data, byte) => data + String.fromCharCode(byte), '' )), 'base64' ); }) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 解决方案三 跟前面方法二一样 添加 {responseType: ‘blob’} 请求头,获取blob类型数据 axios.get( 'http://localhost:801/my/test' , { responseType: 'blob' }).then(res => { console.log( res ) const blob = new Blob([res.data]) var reader = new window.FileReader(); reader.readAsDataURL(blob); reader.onloadend = function () { document.querySelector( '#img' ).src = reader.result } }) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?