uniapp在开发app时上传文件时的问题

手机拍照然后上传没问题 但是在相册中选择的照片上传 ios手机不行 安卓一部分手机也点击没反应

最后对比了下参数 发现路径有所不同

使用uni.saveFile保存路径好在重新上传

javascript 复制代码
  saveFileSync(tempFilePath){
   return new Promise((resolve, reject) => {
     uni.saveFile({
       tempFilePath,
       success: function (file) {
         resolve(file.savedFilePath)
       },
       fail: function (error) {
         reject(error)
       }
     })
   })
 },
javascript 复制代码
    uni.chooseImage({
        count: 1, //默认9
        sizeType: ["compressed"], //可以指定是原图还是压缩图,默认二者都
        sourceType: ['camera','album'], 
         success: async function(result) {
          let ewm = result.tempFiles[0]
          const path = await that.saveFileSync(ewm.path)
          if (result.errMsg === "chooseImage:ok") {
            result.tempFiles[0].path=path
            // that.upload(path);
            that.upload(result.tempFiles[0]);
          } else {
            uni.showToast({
              title: "图片上传失败",
              icon: "none",
            });
          }
        },
        fail(err) {
          uni.showToast({
            title: "取消上传",
            icon: "none",
          });
        },
      });
javascript 复制代码
Upload(event) {
      const token = this.getToken();
      // const url = this.getuploadUrl();
      const imgList = [];
      uni.showLoading({
        title: "上传中...",
        mask: true,
      });
      try {
        const [err, res] = await uni.uploadFile({
          url: `${HOST}/resource/file/upload`,
          filePath: event.path,
          name: "file",
          header: {
            Authorization: token,
          },
        });
       
        if (res && (res.statusCode === 200)) {
          const result = JSON.parse(res.data);
          if (result.code == 200) {
            let res1 = JSON.parse(res.data);
              res1.data.uuid = res1.data.id;
              res1.data.paramskey = event.name;
              imgList.push(res1.data);
              const list = [...this.list, ...imgList];
              this.$emit("value", list);
              this.$emit("change", list);
              this.$emit("upload", imgList);
          } else {
            wx.showToast({
              icon: "none",
              title: result.msg,
            });
          }
        } else {
          wx.showToast({
            icon: "error",
            title: "上传失败",
          });
        }
      } catch (error) {
        console.log(error)
      }
      uni.hideLoading();
      this.$emit("upload", imgList);
    },
       
相关推荐
Moment1 小时前
Vibe Coding 时代,到底该选什么样的工具来提升效率❓❓❓
前端·后端·github
IT_陈寒3 小时前
SpringBoot性能飙升200%?这5个隐藏配置你必须知道!
前端·人工智能·后端
小时前端3 小时前
React性能优化的完整方法论,附赠大厂面试通关技巧
前端·react.js
Nicko4 小时前
Jetpack Compose BOM 2026.02.01 解读与升级指南
前端
小蜜蜂dry4 小时前
nestjs学习 - 控制器、提供者、模块
前端·node.js·nestjs
优秀稳妥的JiaJi4 小时前
基于腾讯地图实现电子围栏绘制与校验
前端·vue.js·前端框架
前端开发呀4 小时前
从 qiankun(乾坤) 迁移到 Module Federation(模块联邦),对MF只能说相见恨晚!
前端
Lee川4 小时前
深度解构JavaScript:作用域链与闭包的内存全景图
javascript·面试
没想好d5 小时前
通用管理后台组件库-10-表单组件
前端
好雨知时节t5 小时前
Pinia中defineStore的使用方法
vue.js