当前位置:首页 > VUE

vue实现上传进度

2026-01-17 08:04:16VUE

Vue 实现文件上传进度

在 Vue 中实现文件上传进度可以通过 XMLHttpRequestaxiosonUploadProgress 事件来监听上传进度。以下是两种常见实现方式:

使用 XMLHttpRequest 实现

通过原生 XMLHttpRequest 对象的 upload 属性监听进度事件:

// 在 Vue 方法中
uploadFile(file) {
  const xhr = new XMLHttpRequest();
  const formData = new FormData();
  formData.append('file', file);

  xhr.upload.addEventListener('progress', (event) => {
    if (event.lengthComputable) {
      const percent = Math.round((event.loaded / event.total) * 100);
      this.uploadProgress = percent; // 更新进度状态
    }
  });

  xhr.open('POST', '/api/upload', true);
  xhr.send(formData);
}

模板中显示进度:

<progress :value="uploadProgress" max="100"></progress>
<span>{{ uploadProgress }}%</span>

使用 Axios 实现

Axios 提供了 onUploadProgress 回调,更适合 Vue 项目:

import axios from 'axios';

// 在 Vue 方法中
async uploadFile(file) {
  const formData = new FormData();
  formData.append('file', file);

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

组件化实现示例

封装一个可复用的上传组件:

<template>
  <div>
    <input type="file" @change="handleFileChange" />
    <button @click="uploadFile">上传</button>
    <div v-if="uploadProgress > 0">
      <progress :value="uploadProgress" max="100"></progress>
      {{ uploadProgress }}%
    </div>
  </div>
</template>

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

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

      try {
        await axios.post('/api/upload', formData, {
          onUploadProgress: (progressEvent) => {
            this.uploadProgress = Math.round(
              (progressEvent.loaded * 100) / progressEvent.total
            );
          },
        });
        this.$emit('upload-success');
      } catch (error) {
        this.$emit('upload-error', error);
      }
    },
  },
};
</script>

注意事项

  1. 跨域问题:确保后端支持 CORS 并正确配置 Access-Control-Allow-Origin
  2. 文件大小限制:后端需处理大文件上传(如分片上传)。
  3. 进度准确性:部分服务器可能无法返回准确的 content-length,需测试验证。
  4. UI 反馈:建议结合加载状态或错误提示(如 v-if="uploadProgress === 100")。

通过上述方法,可以轻松在 Vue 中实现文件上传进度功能。

vue实现上传进度

标签: 进度上传
分享给朋友:

相关文章

vue实现上传

vue实现上传

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

vue实现上传表哥

vue实现上传表哥

Vue 实现上传表格 在 Vue 中实现上传表格功能,通常涉及文件上传、文件解析(如 Excel 或 CSV)以及数据展示。以下是具体实现方法: 使用 Element UI 上传组件 Element…

vue实现头像上传

vue实现头像上传

实现头像上传功能 在Vue中实现头像上传功能通常需要结合文件上传组件和后端接口处理。以下是具体实现步骤: 前端组件实现 创建文件上传组件模板部分: <template> <d…

vue实现视频上传管理

vue实现视频上传管理

视频上传管理实现方案 前端部分(Vue.js) 使用<input type="file">元素配合<video>标签实现基础上传和预览功能: <template>…

h5怎么实现上传视频

h5怎么实现上传视频

使用HTML5实现视频上传 HTML5提供了<input type="file">元素用于文件上传,结合<video>标签可以实现视频上传和预览功能。 <input t…

vue实现上传

vue实现上传

Vue 实现文件上传 在 Vue 中实现文件上传功能可以通过原生 HTML5 的 <input type="file"> 元素结合 Vue 的数据绑定和事件处理来实现。以下是几种常见的实现…