接口返回图片二进制流前端展示
问题描述
请求后台接口时,返回图片内容如下:
咋一看,不是base64格式,二十二进制的图片流数据,前端要想展示,只需要转换数据格式为base64即可。
解决方案
设置responseType为arraybuffer
设置了arraybuffer后的接口返回:
base64转换显示
this.processImgSrc = 'data:image/png;base64,' + btoa(new Uint8Array(res.data).reduce((data, byte) => data + String.fromCharCode(byte), ''))