微信小程序API接入網(wǎng)站后下載頁(yè)面怎么寫?
發(fā)布時(shí)間:2024-01-16 16:28:25
作者:超級(jí)管理員
來(lái)源:WEB
瀏覽量(252)
點(diǎn)贊(483)
摘要:經(jīng)過(guò)3天的研究終于搞定了。。直接上代碼:wxml:<buttondata-download="{{url}}"bindtap="downloadImg">下載</button>JS :downloadImg:function(e){wx.showLoading({title:"加載
經(jīng)過(guò)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)相冊(cè)
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)保存相冊(cè)',
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('打開(kāi)文檔成功')
}
})
}
})掃一掃,關(guān)注我們
483



