当前位置:首页 > 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 或其他工具在服务器端生成缩略图

// 服务器端使用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方案 使用WebRTC技术结合Vue框架可以构建多人视频通话应用。以下是核心实现方法: 技术栈选择 Vue 2/3作为前端框架 WebRTC用于实时通信 Socket.io…

vue 实现视频

vue 实现视频

Vue 实现视频播放功能 使用 Vue 实现视频播放功能可以通过 HTML5 的 <video> 标签或第三方库(如 video.js)来实现。以下是两种常见的方法: 使用 HTML5…

vue实现上传

vue实现上传

Vue 文件上传实现方法 使用 Vue 实现文件上传可以通过原生 HTML5 的 <input type="file"> 元素结合 Vue 的事件处理和数据绑定来实现。 基础实现步骤…

vue 实现上传

vue 实现上传

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

vue上传视频实现

vue上传视频实现

Vue 实现视频上传功能 安装依赖 确保项目中已安装 axios 或 vue-upload-component 等上传库。通过 npm 安装: npm install axios 前端组件实现 创建…

vue视频怎么实现

vue视频怎么实现

实现视频播放功能 在Vue中实现视频播放功能可以通过HTML5的<video>标签或第三方库来实现。以下是几种常见的方法: 使用HTML5 <video>标签 <te…