微信小程序wx.uploadFile上传视频遇到的两个坑

var _this = this;
      wx.chooseVideo({
          sourceType: ['album', 'camera'],
          maxDuration: 60,
          camera: 'back',
          success(res) {
         
              const uploadTask = wx.uploadFile({
                  url: '', // 上传到服务器
                  filePath: res.tempFilePath,
                  name: 'file',
                  header: {
                      "Content-Type": "multipart/form-data"
                  },
                  formData: {
                      fileKey: fileKey
                  },
                  success(res) {
                      const data = JSON.parse(res.data);// 坑一:与wx.request不同,wx.uploadFile返回的是json字符串,需要自己转为JSON对象格式
                      console.log(data);
                      }
              });
              uploadTask.onProgressUpdate((res) => {
                  const uploadProgress = res.progress;
                  if(uploadProgress < 100){
                     // 坑2:wx.uploadFile本身有一个this,所以要通过外部var _this = this 把this带进来
                      _this.setData({
                          uploadPercent: uploadProgress
                      });
                  } else if(uploadProgress === 100){
                      _this.setData({
                          uploadPercent: 50
                      });
                  }
                  console.log('上传进度', res.progress);
                  console.log('已经上传的数据长度', res.totalBytesSent);
                  console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
              });
          }
      })

 

你可能感兴趣的:(微信小程序,wx.uploadFile,小程序)