項(xiàng)目中小程序遇到大圖片生成的需求涛救,需要傳遞一個參數(shù),然后從服務(wù)端獲取生成到的長圖碾褂。微信本身的wx.download()只提供Get請求骑歹,不提供POST請求方式下載。所以使用wx.request請求到arraybuffer存入本地文件。
wx.request({
? ? ? ? url: pathUrl,//請求地址
? ? ? ? method: 'POST',//POST方式
? ? ? ? data: params,//附加參數(shù)
? ? ? ? responseType: 'arraybuffer',//響應(yīng)方式
? ? ? ? header: {
? ? ? ? ? 'content-type': 'application/x-www-form-urlencoded'//我們服務(wù)器都是form
? ? ? ? },
? ? ? ? success(res) {
? ? ? ? ? console.log(res.statusCode)
? ? ? ? ? console.log(res.data)
? ? ? ? ? let fileManager = wx.getFileSystemManager();//獲取文件管理器
? ? ? ? ? let filePath = wx.env.USER_DATA_PATH + '/inner.jpg';//設(shè)置臨時(shí)路徑
? ? ? ? ? fileManager.writeFile({//獲取到的數(shù)據(jù)寫入臨時(shí)路徑
? ? ? ? ? ? filePath: filePath,//臨時(shí)路徑
? ? ? ? ? ? encoding: 'binary',//編碼方式茴晋,二進(jìn)制
? ? ? ? ? ? data: res.data,//請求到的數(shù)據(jù)
? ? ? ? ? ? success: function(res) {
? ? ? ? ? ? ? console.log(res)
? ? ? ? ? ? ? console.log(filePath)//打印路徑
? ? ? ? ? ? ? wx.previewImage({//圖片預(yù)覽
? ? ? ? ? ? ? ? urls: [filePath],
? ? ? ? ? ? ? })
? ? ? ? ? ? ? wx.hideLoading();
? ? ? ? ? ? },
? ? ? ? ? ? fail: function(res) {
? ? ? ? ? ? ? console.log(res)
? ? ? ? ? ? ? wx.hideLoading();
? ? ? ? ? ? },
? ? ? ? ? });
? ? ? ? }
? ? ? })