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

1.web版本

请求中的responseType,需要设置成"arraybuffer"

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

你可能感兴趣的:(后端返回image的Buffer,显示图片)