当前位置:首页 > VUE

vue实现视频上传管理

2026-01-07 02:19:59VUE

Vue 视频上传管理实现方案

前端部分

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

npm install axios element-ui

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

<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)

视频管理功能

视频列表展示

<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 或其他工具在服务器端生成缩略图

vue实现视频上传管理

// 服务器端使用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)
  • 考虑添加视频转码功能以适应不同设备播放
  • 实现断点续传功能提升大文件上传体验

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

相关文章

vue 实现上传

vue 实现上传

Vue 实现文件上传 使用 Vue 实现文件上传可以通过原生 HTML 的 <input type="file"> 或第三方库(如 axios、vue-upload-component)完…

vue实现视频开场

vue实现视频开场

实现视频开场动画的方法 在Vue中实现视频开场动画可以通过多种方式完成,包括使用HTML5的<video>标签、第三方库或CSS动画。以下是几种常见的方法: 使用HTML5 <vi…

vue实现分段上传

vue实现分段上传

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

h5实现抖音刷视频

h5实现抖音刷视频

实现抖音刷视频的H5页面 实现类似抖音的刷视频功能需要结合H5的视频播放、手势交互和无限滚动等技术。以下是关键实现步骤: 视频播放与布局 使用HTML5的<video>标签实现视频播放容…

vue实现视频录制

vue实现视频录制

实现视频录制的基本思路 在Vue中实现视频录制通常需要借助浏览器的MediaDevices API和MediaRecorder API。核心流程包括获取用户摄像头/麦克风权限、录制媒体流、保存录制结果…

vue实现上传封面

vue实现上传封面

Vue 实现上传封面功能 使用原生 input 文件上传 在 Vue 中可以通过原生 HTML 的 input 元素实现文件上传功能。创建一个文件输入框,监听 change 事件获取用户选择的文件。…