记React项目中使用视频上传SDK

2020-06-28  本文已影响0人  我没叫阿
    <!--  IE需要es6-promise -->
    <script src="../lib/es6-promise.min.js"></script>
    <script src="../lib/aliyun-oss-sdk5.2.0.min.js"></script>
    <script src="../aliyun-vod-upload-sdk1.4.0.min.js"></script>
 fileChange(e) {
    console.log(e);
    this.file = e.target.files[0];
    if (!this.file) {
      message.error('请先选择需要上传的文件!');
      return;
    }
    var userData = '{"Vod":{}}';
    if (this.uploader) {
      this.uploader.stopUpload();
    }
    this.uploader = this.createUploader();
    this.uploader.addFile(this.file, null, null, null, userData);
    this.authUpload();
  }
  //开始上传
  authUpload() {
    if (this.uploader !== null) {
      this.uploader.startUpload();
    }
  }
  createUploader() {
    var that = this;
    var uploader = new AliyunUpload.Vod({
      timeout: 60000,
      partSize: 1048576,
      parallel: 5,
      retryCount: 3,
      retryDuration: 2,
      region: 'cn-shanghai',
      userId: '1868162826898636',
      addFileSuccess: function(uploadInfo) {
        console.log(uploadInfo, '添加文件成功', uploadInfo.file.name, uploadInfo.videoId);
        that.videoProgressFun(0.1);
      },
      // 开始上传
      onUploadstarted(uploadInfo) {
        $.ajax({
          type: 'GET',
          contentType: 'application/json;charset=UTF-8',
          url: ` https://app.mobilityclub.cn/admin/admin/video/assumeRole`,
          dataType: 'json',
          headers: {
            token: cookie.get('CMMCSystemToken'),
            version: 'v1',
          },
          success: function(data) {
            console.log(data, '后台获取的上传凭证');
            var accessKeyId = data.body.info.accessKeyId;
            var accessKeySecret = data.body.info.accessKeySecret;
            var secretToken = data.body.info.securityToken;
            uploader.setSTSToken(uploadInfo, accessKeyId, accessKeySecret, secretToken);
          },
        });
      },
      onUploadSucceed: function(uploadInfo) {
        message.success('上传视频成功');
        that.videoIdsaveFun(uploadInfo.videoId);
        that.videoNamesaveFun(uploadInfo.file.name);
        console.log(
          `上传成功:${uploadInfo.file.name},endpoint:${uploadInfo.endpoint},bucket:${
            uploadInfo.bucket
          },object:${uploadInfo.object}`
        );
      },
      onUploadFailed: function(uploadInfo, code, message) {
        console.log('上传失败', uploadInfo, code, message);
      },
      onUploadCanceled: function(uploadInfo, code, message) {
        console.log('取消上传', uploadInfo, code, message);
      },
      onUploadProgress: function(uploadInfo, totalSize, progress) {
        console.log('文件上传进度', uploadInfo, totalSize, progress);
        if(progress == 0){
          progress = 0.2
        }
        that.videoProgressFun(progress);
      },
      onUploadTokenExpired: function(uploadInfo) {
        console.log('上传凭证超时', uploadInfo);
      },
      onUploadEnd: function(uploadInfo) {
        console.log('全部文件上传结束');
      },
    });
    return uploader;
  }
 <input type="file" onChange={this.fileChange.bind(this)} />
上一篇 下一篇

猜你喜欢

热点阅读