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);
    },
       
相关推荐
apcipot_rain2 小时前
【应用密码学】实验五 公钥密码2——ECC
前端·数据库·python
油丶酸萝卜别吃2 小时前
OpenLayers 精确经过三个点的曲线绘制
javascript
ShallowLin2 小时前
vue3学习——组合式 API:生命周期钩子
前端·javascript·vue.js
Nejosi_念旧2 小时前
Vue API 、element-plus自动导入插件
前端·javascript·vue.js
互联网搬砖老肖3 小时前
Web 架构之攻击应急方案
前端·架构
pixle03 小时前
Vue3 Echarts 3D饼图(3D环形图)实现讲解附带源码
前端·3d·echarts
不法3 小时前
uniapp 百家云直播插件打包失败
uni-app·插件使用
麻芝汤圆4 小时前
MapReduce 入门实战:WordCount 程序
大数据·前端·javascript·ajax·spark·mapreduce
juruiyuan1115 小时前
FFmpeg3.4 libavcodec协议框架增加新的decode协议
前端
Peter 谭6 小时前
React Hooks 实现原理深度解析:从基础到源码级理解
前端·javascript·react.js·前端框架·ecmascript