uniapp&&小程序上传pdf文件开发记录

小程序中上传文件时无法获取file对象,只能获取path临时路径

 我们可以使用 wx.chooseMessageFile和wx.uploadFile实现上传文件至服务器

 wx.chooseMessageFile({   // 只能选取微信聊天框中的文件
        count: 1, // 最多选择一个文件
        type: "file", // 选择文件类型
        success: function (res) {
          // 获取文件的本地临时文件路径
          const tempFiles = res.tempFiles[0];
          if (tempFiles.name.indexOf(".pdf") === -1) {
            uni.showToast({
              icon: "none",
              title: "请选择pdf文件上传",
            });
          } else {
            uni.showLoading({
              title: "正在上传",
            });
            wx.uploadFile({
              url: `${baseURL}/rfs/chunkUploadByMobile`,  //服务器地址
              filePath: tempFiles.path,     //文件临时路径
              name: "file",
              header: {
                Authorization: getToken(),
                "Content-Type": "multipart/form-data",
              },
              formData: {
                docName: tempFiles.name,
              },
              success: (res) => {
                   // 上传成功执行
                uni.hideLoading();
                
              },
              fail: (error) => {
                uni.hideLoading();
                console.log(error);
              },
            });
          }
        },
      });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值