el-upload上传视频限制(格式和带宽)

<el-upload
  :before-upload="beforeAvatarUpload">
</el-upload>

beforeAvatarUpload(file) {
      // 获取视频时长
      var url = URL.createObjectURL(file);
      var audioElement = new Audio(url);
      var duration;
      this.durationNumber = audioElement.addEventListener(
        "loadedmetadata",
        function(_event) {
          duration = audioElement.duration; //时长为秒,小数
          return duration;
        }
      );
      setTimeout(() => {
      //视频大小(MB)
        let fileSize = file.size / 1024 / 1024;
        this.bandwidth = fileSize / duration;
      }, 50);
      if (
        [
          "video/mp4",
          "video/ogg",
          "video/flv",
          "video/avi",
          "video/wmv",
          "video/rmvb",
          "video/mov"
        ].indexOf(file.type) == -1
      ) {
        // layer.msg("请上传正确的视频格式");
        this.$message({
          type: "info",
          message: "请上传正确的视频格式"
        });
        return false;
      }
      if (this.bandwidth > 2) {
        // 视频带宽不能超过2MB/s
         this.$message.error('视频带宽过大,上传失败!');
        return false;
      }
      this.isShowUploadVideo = false;
    },

你可能感兴趣的:(【VUE】,【笔记】,音视频,前端,javascript,elementui)