vue3用elementui上传组件上传视频时携带视频长度信息

在项目中有个需求就是要统计视频时长,本来打算在springboot里上传后做,想了半天还是打算在vue里做,原本想的很好,选择视频后将响应式数据中的值设为时长就好了,设置upload组件中onchange事件,用资源链接创建audio对象后再他上面添加监听器,获取对象的duration属性获取时长,然后this.time=duration

<el-upload action="api/uploadVideo" 
                :on-change="logTime">
                <el-button type="primary">更改视频</el-button>
            </el-upload>


logTime(file) {
            var url = URL.createObjectURL(file.raw);
            var audioElement = new Audio(url);
            var duration;
            audioElement.addEventListener("loadedmetadata", function () {
                duration = parseInt(audioElement.duration);
                this.time=duration
            });
        },

        但是发现问题很多,报错说无法对URL使用creatObjectURl,捣鼓了半天发现我这样的想法根本就行不通,就打算还是用手动上传做吧,这时候时长可以获取到了,但是响应式的数据还是设置不了,可能是监听器里用不了?最后还是设置了个hidden的input,通过document.getElementById的方式设置数据,最后提交的时候再从input里取出来

<el-upload action="api/uploadVideo" :data="{ classTopic: classTopic, className: this.class.className }"
                :on-change="logTime" ref="video" :auto-upload="false">
                <el-button type="primary">更改视频</el-button>
            </el-upload>

logTime(file) {
            var url = URL.createObjectURL(file.raw);
            var audioElement = new Audio(url);
            var duration;
            audioElement.addEventListener("loadedmetadata", function () {
                duration = parseInt(audioElement.duration);
                document.getElementById('time').value = duration;
            });
        },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue2中使用ElementUI2上传视频,你需要完成以下步骤: 1. 安装ElementUI2和Vue2 ```bash npm install element-ui@2.15.1 npm install vue@2.6.14 ``` 2. 引入ElementUI2和样式 在你的Vue项目入口文件中,引入ElementUI2和它的CSS文件。 ```js import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) ``` 3. 创建上传组件 在你的Vue组件中,使用ElementUI2的`el-upload`组件创建一个上传组件。你需要设置`action`属性为上传视频的URL,`on-success`属性为上传成功后的回调函数。 ```html <template> <el-upload class="upload-demo" action="/uploadVideo" :on-success="handleUploadSuccess" :on-error="handleUploadError" :before-upload="beforeUpload" :auto-upload="false" :file-list="fileList"> <el-button slot="trigger" size="small" type="primary">选取文件</el-button> <el-button style="margin-left: 10px;" size="small" type="success" @click="handleUpload">上传到服务器</el-button> <div slot="tip" class="el-upload__tip">只能上传mp4文件</div> </el-upload> </template> <script> export default { data() { return { fileList: [] } }, methods: { handleUpload() { this.$refs.upload.submit() }, handleUploadSuccess(response, file, fileList) { console.log(response) }, handleUploadError(error, file, fileList) { console.log(error) }, beforeUpload(file) { const isMp4 = file.type === 'video/mp4' if (!isMp4) { this.$message.error('只能上传mp4文件') } return isMp4 } } } </script> ``` 4. 处理上传视频的后端逻辑 在你的后端服务器上,你需要编写接收上传视频的接口,并将视频保存到服务器上。你可以使用Node.js和Express框架来编写接口,使用`multer`中间件来处理文件上传。 ```js const express = require('express') const multer = require('multer') const app = express() const storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, 'uploads/') }, filename: function (req, file, cb) { cb(null, Date.now() + '-' + file.originalname) } }) const upload = multer({ storage: storage }) app.post('/uploadVideo', upload.single('video'), (req, res) => { const file = req.file if (!file) { const error = new Error('Please upload a file') error.httpStatusCode = 400 return res.send(error) } res.send(file) }) app.listen(3000, () => { console.log('Server started on port 3000') }) ``` 这样,你就可以使用ElementUI2在Vue2中上传视频了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值