uni-app 支付宝云 uploadFile 中 cloudPath 相同不能覆盖

uni-app 支付宝云 uploadFile 中 cloudPath 相同不能覆盖

产品分类:
uniCloud/支付宝小程序云

示例代码:

uniCloud.uploadFile({  
    filePath: this.avatarUrl,  
    cloudPath: `user/avatar/${this.userInfo?._id}}.png`,  
    cloudPathAsRealPath: true,  
    fileType: 'image',  
})

操作步骤:

uniCloud.uploadFile({  
    filePath: this.avatarUrl,  
    cloudPath: `user/avatar/${this.userInfo?._id}}.png`,  
    cloudPathAsRealPath: true,  
    fileType: 'image',  
})  
删除云存储中对应userId的图片,执行上传,返回的结果依旧是被删除的图片

预期结果:

最终结果应该保存的是最新上传的图片,同一个userId有且仅有一张图片

实际结果:

图片始终是最开始上传的那个图片

bug描述:

uniCloud.uploadFile({  
    filePath: this.avatarUrl,  
    cloudPath: `user/avatar/${this.userInfo?._id}}.png`,  
    cloudPathAsRealPath: true,  
    fileType: 'image',  
})  
删除云存储中对应userId的图片,执行上传,返回的结果依旧是被删除的图片  

更多关于uni-app 支付宝云 uploadFile 中 cloudPath 相同不能覆盖的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

在unicloud控制台看下有图片吗?前端访问可能有缓存。

更多关于uni-app 支付宝云 uploadFile 中 cloudPath 相同不能覆盖的实战教程也可以访问 https://www.itying.com/category-93-b0.html


应该是缓存问题,建议上传新图片用新的cloudPath,旧的cloudPath可以执行删除。数据库里保存新的图片地址

这个问题是支付宝云存储的一个特性,相同cloudPath的文件上传时不会自动覆盖。解决方案如下:

  1. 在重新上传前,先调用删除接口清除旧文件:
await uniCloud.deleteFile({
  fileList: [`user/avatar/${this.userInfo?._id}.png`]
})
  1. 或者在上传时添加时间戳保证文件名唯一:
uniCloud.uploadFile({
  filePath: this.avatarUrl,
  cloudPath: `user/avatar/${this.userInfo?._id}_${Date.now()}.png`,
  cloudPathAsRealPath: true,
  fileType: 'image',
})
  1. 也可以使用uniCloud的文件管理API先查询并删除已有文件:
const res = await uniCloud.getTempFileURL({
  fileList: [`user/avatar/${this.userInfo?._id}.png`]
})
if(res.fileList[0].tempFileURL){
  await uniCloud.deleteFile({
    fileList: [`user/avatar/${this.userInfo?._id}.png`]
  })
}
回到顶部