uni-app上传图片到云存储总是失败怎么办?

UP主~金利 阅读 12

我在用uni-app开发一个小程序,想把用户选择的图片上传到uniCloud的云存储,但每次调用uniCloud.uploadFile都报错“file not found”。我明明已经用uni.chooseImage拿到了临时路径,也确认路径不为空,就是传不上去,到底哪里出问题了?

这是我的代码:

<script>
export default {
  methods: {
    async uploadImage() {
      const res = await uni.chooseImage({ count: 1 });
      const tempFilePath = res.tempFilePaths[0];
      console.log('temp path:', tempFilePath); // 能正常打印
      const cloudRes = await uniCloud.uploadFile({
        cloudPath: 'uploads/test.jpg',
        filePath: tempFilePath
      });
      console.log(cloudRes);
    }
  }
}
</script>
我来解答 赞 2 收藏
二维码
手机扫码查看
2 条解答
开心的笔记
你这个问题我见太多了,uni.chooseImage 用 Promise 方式调用时返回的是数组 [err, res],不是直接返回 res 对象。你拿到的 tempFilePath 其实是 undefined,只是你打印的时候没仔细看。

改一下解构方式就行:

async uploadImage() {
const [err, res] = await uni.chooseImage({ count: 1 });
if (err) return;
const tempFilePath = res.tempFilePaths[0];
const cloudRes = await uniCloud.uploadFile({
cloudPath: uploads/${Date.now()}.jpg,
filePath: tempFilePath
});
}


顺便说一句,cloudPath 别写死,不然每次上传都覆盖同一个文件,加个时间戳或者随机字符串。
点赞 1
2026-03-02 12:01
♫秀莲
♫秀莲 Lv1
兄弟,问题出在 uni.chooseImage 的返回值上,它返回的是 [err, res] 数组格式,不是直接给你 res 对象。你打印出来的路径估计根本不是字符串,所以 uploadFile 找不到文件。

改一下解构方式就行:

async uploadImage() {
const [err, res] = await uni.chooseImage({ count: 1 });
if (err) return;
const tempFilePath = res.tempFilePaths[0];
const cloudRes = await uniCloud.uploadFile({
cloudPath: 'uploads/test.jpg',
filePath: tempFilePath
});
console.log(cloudRes);
}


以后遇到这种问题,先把变量类型打印出来看看,别光看值,差不多就行。
点赞 1
2026-03-02 12:00