微信小程序图片下载与保存
下载资源
downFile() { const that = this; var url = this.data.imgUrl; //图片地址 if (!url) return; wx.showLoading({ title: '加载中...' }); wx.downloadFile({ url, success: (res) => { wx.hideToast(); that.saveImage(res.tempFilePath) } }) },
保存图片
saveImage(path) { wx.showLoading({ title: '加载中...' }); wx.saveImageToPhotosAlbum({ filePath: path, }).then(success => { wx.hideLoading(); //隐藏 loading 提示框 console.log("success:", success) wx.showModal({ title: '提示', content: '保存成功', showCancel: false, confirmText: '我知道了' }) }).catch(err => { wx.hideLoading(); console.log("取消", err) var str = err.errMsg if (str.indexOf("authorize no response") > -1 || str.indexOf("auth denied") > -1 || str.indexOf("auth deny") > -1) { this.setWriteAuthorize(); } }) },
系统授权
setWriteAuthorize() { wx.showModal({ title: '授权提示', content: '需要您打开权限才能保存相册', showCancel: false, confirmText: '我知道了', success: () => { wx.openSetting({ success(settingdata) { console.log("settingdata", settingdata) if (settingdata.authSetting['scope.writePhotosAlbum']) { wx.showModal({ title: '提示', content: '获取权限成功,再次点击即可保存', showCancel: false, confirmText: '我知道了' }) } else { wx.showModal({ title: '提示', content: '获取权限失败,将无法保存到相册哦~', showCancel: false, confirmText: '我知道了' }) } }, fail(failData) { console.log("failData", failData) }, complete(finishData) { console.log("finishData", finishData) } }) } }) },

浙公网安备 33010602011771号