element上传视频获取【视频时长】以及文件格式、文件大小限制

html

    <el-upload
      class="upload-demo"
      :action="actionUrl"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
    >
      <button class="ce-button not-hover primary">
        <i class="ce-icon_upload"></i>
        <span>重新上传</span>
      </button>
    </el-upload>

 

 
 methods

    beforeAvatarUpload(file) {
      var fileName = file.name || "";
      var ext = fileName.split(".")[fileName.split(".").length - 1];
      if (
        ext !== "doc" &&
        ext !== "docx" &&
        ext !== "xls" &&
        ext !== "xlsx" &&
        ext !== "ppt" &&
        ext !== "pptx" &&
        ext !== "pdf" &&
        ext !== "mp4"
      ) {
        this.$notify({
          title: "失败",
          message: "上传资源只能是 doc/docx/xls/xlsx/ppt/pptx/pdf/mp4 格式!",
          type: "error",
          duration: 3000
        });
        return false;
      }
      // ppt(10MB),word(10MB),excel(5MB)
      if (ext == "doc" || ext == "docx" || ext == "ppt" || ext == "pptx") {
        debugger;
        if (parseInt(file.size) > parseInt("10485760‬")) {
          this.$notify({
            title: "失败",
            message: "上传word、ppt文件上限为10MB!",
            type: "error",
            duration: 3000
          });
          return false;
        }
      }
      if (ext == "mp4") {
        // 获取视频时长
        var url = URL.createObjectURL(file);
        var audioElement = new Audio(url);
        var duration;
        audioElement.addEventListener("loadedmetadata", function(_event) {
          duration = audioElement.duration; //时长为秒,小数,182.36
          this.$parent.$data.wDuration = parseInt(duration);
          console.log(duration);
        });
      }
      this.$parent.$data.wFileName = file.name;
      this.$parent.$data.wSize = parseFloat(file.size / 1024).toFixed(2); //获取文件大小
    },

1.**URL.createObjectURL() **静态方法会创建一个 DOMString,其中包含一个表示参数中给出的对象的URL。这个 URL 的生命周期和创建它的窗口中的 document 绑定。这个新的URL 对象表示指定的 File 对象或 Blob 对象。(个人感觉可以把对象转换成url使用,十分灵活方便,特别是对于文件对象)。
2.loadedmetadata 当指定的音频/视频的元数据已加载时,会发生 loadedmetadata 事件。音频/视频的元数据包括:时长、尺寸(仅视频)以及文本轨道。

posted on 2019-09-05 16:22  65Seeker  阅读(3165)  评论(0编辑  收藏  举报