后端返回image的Buffer,显示图片

1.web版本
【后端返回image的Buffer,显示图片】请求中的responseType,需要设置成"arraybuffer"

// 获取项目截图 2 getItemPic() { 3this.$http({ 4url: this.$http.adornUrl('/web/showimgFile'), 5method: 'get', 6responseType: "arraybuffer",// 注意:responseType必须是arrayBuffer,json是不行的 7params: this.$http.adornParams({ 8'url' : this.imgId, 9}) 10}).then(({ data }) => { 11let bytes = new Uint8Array(data); 12let storeDatahttps://www.it610.com/article/= ""; 13let len = bytes.byteLength; 14for (let i = 0; i < len; i++) { 15storeData += String.fromCharCode(bytes[i]); 16} 17this.imgUrl = "data:image/png; base64," + window.btoa(storeData); 18}); 19 }

2.小程序版本
const fsm = wx.getFileSystemManager(); const FILE_BASE_NAME = 'qrcode_base64src'; const filePath = wx.env.USER_DATA_PATH + '/' + FILE_BASE_NAME + '.jpg'; fsm.writeFile({ filePath, data: res, encoding: 'binary', success() { that.setData({ wxCode: filePath //结果图片 }) }, fail() {}, })

    推荐阅读