小程序的buffer转图片

来源:博客园 分类: 文章浏览史 发布时间:2020-08-13 11:49:37 最后更新:2020-08-13 浏览:445
转载声明:
本文为摘录自“博客园”,版权归原作者所有。
温馨提示:
为了更好的体验,请点击原文链接进行浏览
摘录时间:
2020-08-13 11:49:37
项目中小程序遇到大图片生成的需求,需要传递一个参数,然后从服务端获取生成到的长图。微信本身的wx.download()只提供Get请求,不提供POST请求方式下载。所以使用wx.request请求到arraybuffer存入本地文件。


复制代码
wx.request({

        url: pathUrl,//请求地址

        method: 'POST',//POST方式

        data: params,//附加参数

        responseType: 'arraybuffer',//响应方式

        header: {

          'content-type': 'application/x-www-form-urlencoded'//我们服务器都是form

        },

        success(res) {

          console.log(res.statusCode)

          console.log(res.data)

          let fileManager = wx.getFileSystemManager();//获取文件管理器

          let filePath = wx.env.USER_DATA_PATH + '/inner.jpg';//设置临时路径

          fileManager.writeFile({//获取到的数据写入临时路径

            filePath: filePath,//临时路径

            encoding: 'binary',//编码方式,二进制

            data: res.data,//请求到的数据

            success: function(res) {

              console.log(res)

              console.log(filePath)//打印路径

              wx.previewImage({//图片预览

                urls: [filePath],

              })

              wx.hideLoading();

            },

            fail: function(res) {

              console.log(res)

              wx.hideLoading();

            },

          });

        }

      })
复制代码

 

 

 

 

 

转 : https://www.jianshu.com/p/45e87673d5c6

php技术微信