当前位置:首页 > VUE

vue实现压缩上传文件

2026-02-25 19:48:33VUE

压缩上传文件的实现方法

使用HTML5的File API和Canvas进行压缩

前端可以使用HTML5的File API读取文件,对于图片文件可以利用Canvas进行压缩。通过Canvas的drawImagetoDataURL方法调整图片质量,实现压缩效果。

function compressImage(file, quality = 0.8) {
  return new Promise((resolve) => {
    const reader = new FileReader()
    reader.onload = (event) => {
      const img = new Image()
      img.onload = () => {
        const canvas = document.createElement('canvas')
        const ctx = canvas.getContext('2d')
        canvas.width = img.width
        canvas.height = img.height
        ctx.drawImage(img, 0, 0, canvas.width, canvas.height)
        canvas.toBlob((blob) => {
          resolve(new File([blob], file.name, { type: 'image/jpeg' }))
        }, 'image/jpeg', quality)
      }
      img.src = event.target.result
    }
    reader.readAsDataURL(file)
  })
}

使用第三方库进行更高效的压缩

对于更复杂的压缩需求,可以使用compressorjs等专业压缩库。这些库提供了更多压缩选项和更好的兼容性。

vue实现压缩上传文件

import Compressor from 'compressorjs'

function compressWithLibrary(file) {
  return new Promise((resolve) => {
    new Compressor(file, {
      quality: 0.6,
      success(result) {
        resolve(result)
      },
      error(err) {
        console.error(err.message)
        resolve(file) // 压缩失败返回原文件
      }
    })
  })
}

文件上传实现

压缩后的文件可以通过axios或其他HTTP库上传到服务器。创建FormData对象并附加压缩后的文件。

vue实现压缩上传文件

async function uploadFile(file) {
  const compressedFile = await compressImage(file)
  const formData = new FormData()
  formData.append('file', compressedFile)

  try {
    const response = await axios.post('/upload', formData, {
      headers: { 'Content-Type': 'multipart/form-data' }
    })
    console.log('上传成功', response.data)
  } catch (error) {
    console.error('上传失败', error)
  }
}

进度显示和限制文件大小

上传过程中可以显示进度条,并在压缩前检查文件大小,避免不必要的压缩操作。

function checkFileSize(file, maxSizeMB = 5) {
  const maxSizeBytes = maxSizeMB * 1024 * 1024
  return file.size <= maxSizeBytes
}

// 上传带进度显示
function uploadWithProgress(file, onProgress) {
  const config = {
    onUploadProgress: (progressEvent) => {
      const percent = Math.round((progressEvent.loaded * 100) / progressEvent.total)
      onProgress(percent)
    }
  }
  return axios.post('/upload', file, config)
}

完整组件示例

在Vue组件中整合上述功能,提供完整的文件选择和上传体验。

<template>
  <div>
    <input type="file" @change="handleFileChange" />
    <button @click="upload" :disabled="!file">上传</button>
    <progress v-if="progress >= 0" :value="progress" max="100"></progress>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  data() {
    return {
      file: null,
      progress: -1
    }
  },
  methods: {
    async handleFileChange(event) {
      this.file = event.target.files[0]
    },
    async upload() {
      if (!this.file) return

      const compressedFile = await this.compressImage(this.file)
      const formData = new FormData()
      formData.append('file', compressedFile)

      try {
        await axios.post('/upload', formData, {
          onUploadProgress: (progressEvent) => {
            this.progress = Math.round(
              (progressEvent.loaded * 100) / progressEvent.total
            )
          }
        })
        this.$emit('upload-success')
      } catch (error) {
        console.error('上传失败', error)
      } finally {
        this.progress = -1
      }
    },
    compressImage(file) {
      // 实现压缩逻辑
    }
  }
}
</script>

标签: 上传文件vue
分享给朋友:

相关文章

vue 实现在线预览

vue 实现在线预览

Vue 实现在线预览的常见方法 使用 iframe 嵌入 通过 iframe 标签可以嵌入多种类型的文件,如 PDF、图片、网页等。需要确保文件地址可访问。 <template> &…

vue实现主题

vue实现主题

Vue 实现主题的方法 使用 CSS 变量动态切换主题 定义全局 CSS 变量,通过修改这些变量实现主题切换。在 Vue 的根组件或全局样式中定义变量: :root { --primary-co…

vue实现socket

vue实现socket

Vue 中实现 WebSocket 通信 在 Vue 项目中实现 WebSocket 通信可以通过原生 WebSocket API 或第三方库(如 socket.io-client)完成。以下是两种常…

vue实现active

vue实现active

Vue 实现 active 状态的方法 在 Vue 中实现 active 状态可以通过以下几种方式实现,具体取决于使用场景和需求。 使用 v-bind:class 动态绑定类名 通过 v-bind:…

vue 实现视频

vue 实现视频

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

vue实现分发

vue实现分发

Vue 实现分发的方法 在 Vue 中,分发(Distribution)通常指的是将内容从父组件传递到子组件,或者通过插槽(Slots)机制让父组件控制子组件的部分渲染内容。以下是几种常见的实现分发的…