uniapp 微信小程序下载文件 完整方法@TOC
//1. 获取相册授权
getSettingFun() {
wx.getSetting({
success(res) {
if (!res.authSetting['scope.writePhotosAlbum']) {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success() {
console.log('授权成功')
}
})
}
}
})
},
//2. 保存图片/视频到本地
dlImg(type) {
let url = null;
let api = null;
// 保存视频到本地
if( type == 'video' ){
url = this.data.url;
api = 'saveVideoToPhotosAlbum';
}
// 保存图片到本地
if( type == 'image' ){
url = this.data.cover;
api = 'saveImageToPhotosAlbum';
}
uni.showLoading({
title: '加载中..'
});
wx.downloadFile({
url, //图片地址
success: (res) => {
console.log(res);
//图片保存到本地
// saveVideoToPhotosAlbum
wx[api]({
filePath: res.tempFilePath,
success: (data) => {
wx.hideLoading()
wx.showModal({
title: '提示',
content: type=='video'?'短视频保存成功~':'封面保存成功~',
showCancel: false,
});
this.$refs.uToast.close();
},
fail: (err) => {
if (err.errMsg === api+":fail:auth denied" ||
err.errMsg === api+":fail auth deny" ||
err.errMsg === api+":fail authorize no response") {
// 这边微信做过调整,必须要在按钮中触发,因此需要在弹框回调中进行调用
wx.showModal({
title: '提示',
content: '需要您授权保存相册',
showCancel: false,
success: modalSuccess => {
wx.openSetting({
success(settingdata) {
console.log("settingdata",
settingdata)
if (settingdata
.authSetting[
'scope.writePhotosAlbum'
]) {
wx.showModal({
title: '提示',
content: '获取权限成功,再次点击保存海报即可保存',
showCancel: false,
})
} else {
wx.showModal({
title: '提示',
content: '获取权限失败,将无法保存到相册哦~',
showCancel: false,
})
}
},
fail(failData) {
console.log("failData",
failData);
},
complete(finishData) {
console.log("finishData",
finishData);
}
})
}
})
}
},
complete(res) {
wx.hideLoading()
}
})
}
})
setTimeout(() => {
uni.hideLoading();
}, 5000)
}
版权声明:本文为weixin_44047819原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。