微信小程序点击下载按钮,下载多图到手机
视图代码 index.wxml
<button bindtap="baocunImgs">下载保存到手机</button>
index.js代码
Page({
data: {
},
onLoad: function(options) {
},
// 点击下载
baocunImgs() {
var imglist=[
‘https://s1.ax1x.com/2020/06/02/tY3JmQ.jpg‘,
‘https://s1.ax1x.com/2020/06/02/tY38Og.jpg‘
]
var that=this
wx.getSetting({
success(res) {
if (!res.authSetting[‘scope.writePhotosAlbum‘]) {
wx.authorize({
scope:‘scope.writePhotosAlbum‘,
success(res) {
imglist.forEach(function (item, index) {
that.downloadImage(item)
})
}
})
}else{
imglist.forEach(function (item, index) {
console.log(item)
that.downloadImage(item)
})
}
}
})
},
// 下载文件
downloadImage: function(imageUrl) {
wx.downloadFile({
url: imageUrl,
success: function (res) {
console.log("下载文件:success");
console.log(res);
// 保存图片到系统相册
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success(res) {
console.log("保存图片:success");
wx.showToast({
title: ‘保存成功‘,
});
},
fail(res) {
console.log("保存图片:fail");
console.log(res);
}
})
},
fail: function (res) {
console.log("下载文件:fail");
console.log(res);
}
})
}
})