小程序wx.uploadFile上传视频报错
发布于 5 年前 作者 xiulan98 2002 次浏览 来自 官方Issues

小程序wx.uploadFile上传视频报错:

开发工具上传时,报错如下:

真机调试中,报错如下:

安卓版本号:Android10;

手机:小米8

2 回复

这个url参数没看懂,又封装了?

这么写能不报错吗,还以为你说的是bug呢

//读取文件
  afterRead(e) {
    console.log(e.detail);
    const file = e.detail.file; 
    const index = file.url.lastIndexOf('.');
    const type = file.url.substring(index + 1, file.url.length);
    const flag = type == 'mp4'||type == 'mp3';
    if(!flag){
      app.showToast('文件格式只支持mp4、mp3''none');
      return false;
    }

    this.setData({
      file: file,
      isNoFilefalse
    });

    this.uploadCloudFile(file.url);
  },

  //上传文件接口
  uploadCloudFile(filePath){
    console.log(filePath)
    app.api.uploadCloudFile(filePath).then((res)=>{
      console.log('上传成功',res);
    }).catch((err)=>{
      console.log('上传失败',err);
    });
  },
 <van-uploader max-size="{{200*1024*1024}}" bind:oversize="oversize" bind:after-read="afterRead"  accept="video">
          <van-button type="info" size="small">上传视频文件</van-button>
          <view class="upload-tip">格式:mp4</view>
          <view class="err-tip" wx:if="{{isNoFile}}">请上传音视频文件</view>
        </van-uploader>
  
//api
 uploadCloudFile(filePath) {
    return http.upload({
      url'/attacheFile/uploadFile',
      filePath: filePath,
      name'file'
    });
  },

//http
upload({
    url,
    filePath,
    name,
    formData = {}
  }) {
    return new Promise((resolve, reject) => {
      wx.uploadFile({
        url: url,
        filePath: filePath,
        name: name,
        formData: formData,
        success(res) {
          let data = JSON.parse(res.data);
          if (data.status != 0) {
            reject(data.message)
          } else {
            resolve(data.data)
          }
        },
        fail(error) => {
          reject(error)
        }
      })
    });
  }
        
回到顶部