项目中小程序遇到大图片生成的需求,需要传递一个参数,然后从服务端获取生成到的长图。微信本身的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