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 事件。音頻/視頻的元數據包括:時長、尺寸(僅視頻)以及文本軌道。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM