微信小程序API接入网站后下载页面怎么写?
经过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保存图片到系统相册
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath, // 图片文件路径
success:function(data) {
wx.hideLoading() // 隐藏loading 提示框
wx.showModal({
title: '提示',
content: '保存成功',
modalType:false
})
},
// 接口调用失败案例
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: '需要您授权保存相册',
modalType: false,
success:modalSuccess => {
wx.openSetting({
success(settingdata) {
console.log("settingdata",settingdata)
if(settingdata.authSetting['scope.writePhotosAlbum']){
wx.showModal({
title: '提示',
content: '获取权限成功,再次点击图片即可保存',
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('打开文档成功')
}
})
}
})上一篇:禁止联网的方法,其它软件适用






