vue 获取视频时长
程序员文章站
2022-07-01 23:33:57
参考资料:js获取上传音视频文件的时长 直接通过element-ui自带的上传组件结合js即可,代码如下: HTML: 得加一个中间字段,用于保存视频时长的值,因为在监听器里面不能直接使用vue页面定义的全局变量: js: ......
参考资料:
直接通过element-ui自带的上传组件结合js即可,代码如下:
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>
得加一个中间字段,用于保存视频时长的值,因为在监听器里面不能直接使用vue页面定义的全局变量:
<input type="text" ref="dur" name='0'/>
js:
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; var ele = this.$refs.dur audioelement.addeventlistener("loadedmetadata", function (_event) { duration = audioelement.duration; //时长为秒,小数,182.36 ele.name = duration //目前使用这种方式解决js与vue之间的传值问题 // this.$parent.$data.wduration = parseint(duration) //注意传递给接口的时长字段类型要与接口接收的字段类型一致,不然无法获取到值 }); } this.$parent.$data.wfilename = file.name this.$parent.$data.wsize = parsefloat(file.size / 1024).tofixed(2) //获取文件大小 }