在微信小程序中,云开发上传并没有一个一次性上传多张图片的方法,只能通过将图片放在数组,然后通过操作数组的方式来一张一张的上传以及控制上传的图片`
wx.cloud.uploadFile({
cloudPath: 'example.png', // 上传至云端的路径
filePath: '', // 小程序临时文件路径
success: res => {
// 返回文件 ID
console.log(res.fileID)
},
fail: console.error
})
其中要注意的点有
1.文件的cloudPath是不能重复的,起名的时候可以使用时间戳加上文件名匹配例如
var cloudPath = 'actimg/' +new Date().getTime() + this.data.openid + this.data.imgList[i].match(/\.[^.]+?$/)[0]
这里使用的是***储存的文件夹+一个唯一的时间戳+上传人的openid+这张图片的后缀名***
2.如果要上传多张图片可以使用数组遍历,一张一张上传``
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: console.error
})}
})
先使用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来进行删除
如果这篇文章对你有帮助的话,希望点个赞噢