Vue2上传视频并自动播放

本文介绍了一种使用HTML5技术实现文件上传并实时预览视频的方法。通过JavaScript监听文件输入变化,获取用户选择的视频文件,并利用File API创建对象URL来实现视频的即时播放。这种方式无需服务器端处理即可让用户预览所选文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<template>
   <div>
      <input type="file" id="file" @change="onInputFileChange()">
      <video id="video_id"  width="520" height="360" controls autoplay loop>你的浏览器不能支持HTML5视频</video>
   </div>
</template>

<script>
export default {
   name: "shipin",
   methods: {
      onInputFileChange() {
         console.log("上传----")
         var file = document.getElementById('file').files[0];
         var url = URL.createObjectURL(file);
         console.log(url);
         document.getElementById("video_id").src = url;
      }
   },
}
</script>

<style scoped>

</style>
### 如何使用 Spring Boot 和 Vue 实现视频上传与播放功能 #### 项目准备 为了实现视频上传和播放功能,在前端部分采用 `Vue` 框架配合 `vue-video-player` 插件,而后端则利用 `Spring Boot` 来处理文件存储逻辑以及提供API支持。 #### 后端 API 设计 在 `Spring Boot` 中定义用于接收客户端发送过来的视频文件的服务接口。通常情况下会涉及到两个主要的操作:一个是上传视频文件至服务器;另一个是从服务器获取已上传视频的信息以便于前端调用显示[^4]。 ```java @RestController @RequestMapping("/api/videos") public class VideoController { @PostMapping("/upload") public ResponseEntity<String> uploadVideo(@RequestParam("file") MultipartFile file){ try { // 处理文件保存业务... return new ResponseEntity<>("File uploaded successfully", HttpStatus.OK); } catch (Exception e) { return new ResponseEntity<>(e.getMessage(), HttpStatus.EXPECTATION_FAILED); } } @GetMapping("/{id}") public ResponseEntity<Resource> getVideoById(@PathVariable String id){ // 根据ID加载资源... return null; } } ``` #### 前端页面布局 对于 `Vue` 应用来说,则需要设置好表单用来提交视频文件给后端,且当接收到响应之后能够及时更新视图中的数据列表。同时还要引入 `vue-video-player` 组件来负责实际的媒体流解析工作[^1]。 ```html <template> <div> <!-- 文件选择器 --> <input type="file" accept="video/*" v-on:change="handleFileChange"/> <!-- 提交按钮触发上传动作 --> <button @click="submitUpload">上传</button> <!-- 显示所有可用视频 --> <div v-for="(item, index) in videoList" :key="index"> <video-player ref="player" :options="getOptions(item.url)"> </video-player> </div> </div> </template> <script> import { VideoPlayer } from 'vue-video-player'; export default { components:{ VideoPlayer, }, data(){ return{ selectedFile:null, videoList:[] }; }, methods:{ handleFileChange(event){ this.selectedFile=event.target.files[0]; }, async submitUpload(){ const formData=new FormData(); formData.append('file',this.selectedFile); await fetch('/api/videos/upload',{ method:'POST', body:formData }).then(response=>response.json()) .then(data=>{ console.log('Success:',data); this.videoList.push({url:`/api/videos/${data.id}`}); }) .catch(error =>console.error('Error:',error)); }, getOptions(url){ return{ sources:[ {type:"video/mp4", src:url}, ], autoplay:true, loop:true }; } } }; </script> ``` 上述代码片段展示了如何在一个简单的 `Vue` 单页应用中集成视频上传及自动循环播放的功能。需要注意的是这里只是给出了基本框架结构示意,具体细节还需要根据实际情况调整优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值