问题描述
在微信小程序中,云开发上传并没有一个一次性上传多张图片的方法,只能通过将图片放在数组,然后通过操作数组的方式来一张一张的上传以及控制上传的图片`
wx.cloud.uploadFile({
cloudPath: 'example.png', // 上传至云端的路径
filePath: '', // 小程序临时文件路径
success: res => {
// 返回文件 ID
console.log(res.fileID)
},
fail: console.error
})
注意点
文件的cloudPath是不能重复的,起名的时候可以使用时间戳加上文件名匹配,例如
var cloudPath = 'actimg/' +new Date().getTime() + this.data.openid + this.data.imgList[i].match(/\.[^.]+?$/)[0]
这里使用的是储存的文件夹+一个唯一的时间戳+上传人的openid+这张图片的后缀名
上传多张图片
for (let i in this.data.imgList)
{var cloudPath = 'actimg/' +new Date().getTime() + this.data.openid + this.data.imgList[i].match(/\.[^.]+?$/)[0]
wx.cloud.uploadFile({
cloudPath,
filePath,
complete: resa => {
var temp = this.data.cloudimg
temp.push(resa.fileID)
this.setData({ cloudimg: temp })
})
}
}
保存至相册
savesignupimg(){
wx.authorize({
scope: 'scope.writePhotosAlbum',
success:res0=>{wx.cloud.downloadFile({
fileID: this.data.actarry.groupimg, // 文件 ID
success: res => {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success:sres=>{
this.setData({
signup:false
})
}
})
},
fail:err=>{console.log(err)}
})}
})
先使用wx.authorize()
获取活动的手机相册权限,成功后使用wx.cloud.downloadFile()
方法,fileID使用你在上传成功时回调中的fileID,这样可以在回调中获取一个临时的图片文件,再通过wx.saveImageToPhotosAlbum()
方法来将图片放入用户的手机相册
删除操作
wx.cloud.deleteFile({
fileList: ['a7xzcb'],
success: res => {
// handle success
console.log(res.fileList)
},
fail: console.error
})
这里的fileList中也是填写文件的fileId来进行删除