小程序 arraybuffer轉(zhuǎn)圖片

項(xiàng)目中小程序遇到大圖片生成的需求,需要傳遞一個(gè)參數(shù),然后從服務(wù)端獲取生成到的長(zhǎng)圖。微信本身的wx.download()只提供Get請(qǐng)求,不提供POST請(qǐng)求方式下載。所以使用wx.request請(qǐng)求到arraybuffer存入本地文件。

wx.request({

? ? ? ? url: pathUrl,//請(qǐng)求地址

? ? ? ? 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,//請(qǐng)求到的數(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();

? ? ? ? ? ? },

? ? ? ? ? });

? ? ? ? }

? ? ? })

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容