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

手机拍照然后上传没问题 但是在相册中选择的照片上传 ios手机不行 安卓一部分手机也点击没反应
最后对比了下参数 发现路径有所不同
使用uni.saveFile保存路径好在重新上传

  saveFileSync(tempFilePath){
   return new Promise((resolve, reject) => {
     uni.saveFile({
       tempFilePath,
       success: function (file) {
         resolve(file.savedFilePath)
       },
       fail: function (error) {
         reject(error)
       }
     })
   })
 },
    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",
          });
        },
      });
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);
    },
       
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值