微信小程序API接入網(wǎng)站后下載頁面怎么寫?
發(fā)布時間:2024-01-16 16:28:25
作者:超級管理員
來源:WEB
瀏覽量(212)
點(diǎn)贊(467)
摘要:經(jīng)過3天的研究終于搞定了。。直接上代碼:wxml:<buttondata-download="{{url}}"bindtap="downloadImg">下載</button>JS :downloadImg:function(e){wx.showLoading({title:"加載
經(jīng)過3天的研究
終于搞定了。。直接上代碼:
wxml:
<button data-download="{{url}}" bindtap="downloadImg">下載</button>
JS :
downloadImg: function (e) { wx.showLoading({ title:"加載中" }); // wx.downloadFile方法:下載文件到本地 wx.downloadFile({ url:e.currentTarget.dataset.download, success:function(res) { // wx.saveImageToPhotosAlbum保存圖片到系統(tǒng)相冊 wx.saveImageToPhotosAlbum({ filePath: res.tempFilePath, // 圖片文件路徑 success:function(data) { wx.hideLoading() // 隱藏loading 提示框 wx.showModal({ title: '提示', content: '保存成功', modalType:false }) }, // 接口調(diào)用失敗案例 fail:function(err) { if(err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg === "saveImageToPhotosAlbum:fail authorize no response"){ wx.showModal({ title: '提示', content: '需要您授權(quán)保存相冊', modalType: false, success:modalSuccess => { wx.openSetting({ success(settingdata) { console.log("settingdata",settingdata) if(settingdata.authSetting['scope.writePhotosAlbum']){ wx.showModal({ title: '提示', content: '獲取權(quán)限成功,再次點(diǎn)擊圖片即可保存', modalType: false, }) } }, }) } }) } }, complete(res) { wx.hideLoading() // 隱藏loading提示框 } }) } }) }
方法二:
JS:
wx.downloadFile({ url: e.currentTarget.dataset.download, success: function(res) { const filePath = res.tempFilePath wx.openDocument({ filePath: filePath, success: function(res) { console.log('打開文檔成功') } }) } })
掃一掃,關(guān)注我們
467