当前位置:首页 > VUE

vue实现视频上传管理

2026-01-07 02:19:59VUE

Vue 视频上传管理实现方案

前端部分

安装依赖 需要安装 axios 用于网络请求,element-ui 或其他 UI 库提供上传组件(可选)

npm install axios element-ui

基础上传组件实现 使用 <input type="file"> 或 UI 库的上传组件

vue实现视频上传管理

<template>
  <div>
    <input type="file" @change="handleFileChange" accept="video/*">
    <button @click="uploadVideo">上传</button>
    <progress v-if="progress" :value="progress" max="100"></progress>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedFile: null,
      progress: 0
    }
  },
  methods: {
    handleFileChange(event) {
      this.selectedFile = event.target.files[0]
    },
    async uploadVideo() {
      if (!this.selectedFile) return

      const formData = new FormData()
      formData.append('video', this.selectedFile)

      try {
        const response = await axios.post('/api/upload', formData, {
          onUploadProgress: progressEvent => {
            this.progress = Math.round(
              (progressEvent.loaded * 100) / progressEvent.total
            )
          }
        })
        console.log('上传成功', response.data)
      } catch (error) {
        console.error('上传失败', error)
      }
    }
  }
}
</script>

后端接口处理(Node.js示例)

Express 接收文件

const express = require('express')
const multer = require('multer')
const path = require('path')

const app = express()
const upload = multer({ dest: 'uploads/' })

app.post('/api/upload', upload.single('video'), (req, res) => {
  // 处理上传的视频文件
  const fileInfo = {
    originalName: req.file.originalname,
    savedPath: req.file.path,
    size: req.file.size,
    mimetype: req.file.mimetype
  }

  // 保存文件信息到数据库
  // ...

  res.json({ success: true, file: fileInfo })
})

app.listen(3000)

视频管理功能

视频列表展示

vue实现视频上传管理

<template>
  <div>
    <div v-for="video in videos" :key="video.id">
      <video :src="video.url" controls width="300"></video>
      <p>{{ video.name }}</p>
      <button @click="deleteVideo(video.id)">删除</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      videos: []
    }
  },
  async created() {
    await this.fetchVideos()
  },
  methods: {
    async fetchVideos() {
      const response = await axios.get('/api/videos')
      this.videos = response.data
    },
    async deleteVideo(id) {
      await axios.delete(`/api/videos/${id}`)
      this.fetchVideos()
    }
  }
}
</script>

高级功能实现

分片上传(大文件支持)

async uploadChunks(file) {
  const chunkSize = 5 * 1024 * 1024 // 5MB
  const chunks = Math.ceil(file.size / chunkSize)

  for (let i = 0; i < chunks; i++) {
    const start = i * chunkSize
    const end = Math.min(start + chunkSize, file.size)
    const chunk = file.slice(start, end)

    const formData = new FormData()
    formData.append('chunk', chunk)
    formData.append('chunkIndex', i)
    formData.append('totalChunks', chunks)
    formData.append('fileId', this.fileId)

    await axios.post('/api/upload-chunk', formData)
    this.progress = Math.round(((i + 1) / chunks) * 100)
  }

  await axios.post('/api/merge-chunks', { 
    fileId: this.fileId,
    fileName: file.name
  })
}

视频预览生成 使用 FFmpeg 或其他工具在服务器端生成缩略图

// 服务器端使用fluent-ffmpeg
const ffmpeg = require('fluent-ffmpeg')

app.post('/api/generate-thumbnail', (req, res) => {
  const videoPath = req.body.videoPath
  const outputPath = `thumbnails/${Date.now()}.jpg`

  ffmpeg(videoPath)
    .screenshots({
      timestamps: ['00:00:01'],
      filename: outputPath,
      folder: './public'
    })
    .on('end', () => {
      res.json({ thumbnail: `/public/${outputPath}` })
    })
})

注意事项

  • 前端需要处理文件类型和大小验证
  • 后端需要设置合适的文件存储位置和访问权限
  • 对于生产环境,建议使用云存储服务(如AWS S3、阿里云OSS)
  • 考虑添加视频转码功能以适应不同设备播放
  • 实现断点续传功能提升大文件上传体验

标签: 上传视频
分享给朋友:

相关文章

css飞机制作视频

css飞机制作视频

使用CSS制作飞机动画 通过CSS的动画和变形属性,可以创建一个简单的飞机动画效果。以下是实现步骤: HTML结构 <div class="airplane"></div…

h5实现视频通话

h5实现视频通话

WebRTC 基础实现 H5 视频通话主要通过 WebRTC(Web Real-Time Communication)技术实现。现代浏览器原生支持该 API,无需插件即可完成点对点音视频传输。 安…

vue 实现视频

vue 实现视频

Vue 实现视频播放的方法 在 Vue 中实现视频播放可以通过多种方式完成,常见的有使用 HTML5 的 <video> 标签或第三方库如 video.js。以下是几种实现方法: 使用…

vue 实现上传

vue 实现上传

Vue 实现文件上传的方法 在 Vue 中实现文件上传可以通过多种方式完成,以下是几种常见的方法: 使用原生 HTML 的 input 元素 通过 HTML 的 input 元素设置 type="…

vue实现分段上传

vue实现分段上传

实现分段上传的基本思路 分段上传(分片上传)是将大文件分割成多个小块分别上传,最后在服务器合并的技术。这种方式能提高上传稳定性、支持断点续传,并减轻服务器压力。 前端实现步骤 安装依赖 需要axio…

vue 怎么实现上传

vue 怎么实现上传

Vue 实现文件上传的方法 使用原生 HTML5 文件上传 通过 <input type="file"> 元素结合 Vue 的数据绑定实现基础文件上传功能。 <template&g…