当前位置: 代码网 > it编程>编程语言>Javascript > uniapp在开发app时上传文件时的问题记录

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

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

手机拍照然后上传没问题 但是在相册中选择的照片上传 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);
    },

到此这篇关于uniapp在开发app时上传文件时的问题的文章就介绍到这了,更多相关uniapp上传文件内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com