項目中小程序遇到大圖片生成的需求,需要傳遞一個參數,然后從服務端獲取生成到的長圖。微信本身的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
