当前位置:首页 > VUE

vue前端实现下载进度

2026-01-12 07:49:57VUE

Vue 前端实现下载进度的方法

使用 axios 的 onDownloadProgress 回调

在 axios 请求中,可以通过 onDownloadProgress 回调函数实时获取下载进度。该回调会提供一个事件对象,包含 loaded(已下载字节)和 total(总字节)属性。

axios.get('/file-url', {
  responseType: 'blob',
  onDownloadProgress: (progressEvent) => {
    const percentCompleted = Math.round(
      (progressEvent.loaded * 100) / progressEvent.total
    );
    console.log(percentCompleted + '%');
  }
}).then(response => {
  // 处理下载完成后的逻辑
});

结合 Vue 的响应式数据更新进度条

将进度数据绑定到 Vue 的 data 中,通过模板或计算属性实时显示进度条。

export default {
  data() {
    return {
      downloadProgress: 0
    };
  },
  methods: {
    downloadFile() {
      axios.get('/file-url', {
        responseType: 'blob',
        onDownloadProgress: (progressEvent) => {
          this.downloadProgress = Math.round(
            (progressEvent.loaded * 100) / progressEvent.total
          );
        }
      }).then(response => {
        // 创建下载链接
        const url = window.URL.createObjectURL(new Blob([response.data]));
        const link = document.createElement('a');
        link.href = url;
        link.setAttribute('download', 'filename.ext');
        document.body.appendChild(link);
        link.click();
      });
    }
  }
};

使用进度条组件

结合 UI 框架(如 Element UI、Ant Design Vue)的进度条组件,实现可视化效果。

<template>
  <div>
    <el-progress :percentage="downloadProgress"></el-progress>
    <button @click="downloadFile">下载文件</button>
  </div>
</template>

处理跨域和分块下载

对于大文件或分块下载,需确保服务器支持 Content-Length 头信息。若服务器未返回 total,需手动计算或分块处理。

onDownloadProgress: (progressEvent) => {
  if (progressEvent.lengthComputable) {
    this.downloadProgress = Math.round(
      (progressEvent.loaded * 100) / progressEvent.total
    );
  } else {
    // 无法计算总大小时的备选方案
    this.downloadProgress = Math.round(
      (progressEvent.loaded / estimatedTotal) * 100
    );
  }
}

使用 Fetch API 替代方案

若未使用 axios,可通过 Fetch API 的 ReadableStream 实现类似功能。

fetch('/file-url')
  .then(response => {
    const reader = response.body.getReader();
    const contentLength = +response.headers.get('Content-Length');
    let receivedLength = 0;

    return new Promise((resolve) => {
      function processChunk({ done, value }) {
        if (done) {
          resolve(new Blob([chunks]));
          return;
        }
        chunks.push(value);
        receivedLength += value.length;
        this.downloadProgress = Math.round((receivedLength / contentLength) * 100);
        return reader.read().then(processChunk);
      }
      return reader.read().then(processChunk);
    });
  });

注意事项

  • 确保服务器正确返回 Content-Length 头,否则 total 可能为 0。
  • 进度计算需处理 lengthComputablefalse 的情况。
  • 大文件下载建议使用分块或流式处理,避免内存问题。

vue前端实现下载进度

标签: 进度vue
分享给朋友:

相关文章

vue实现菜单

vue实现菜单

Vue 实现菜单的方法 在 Vue 中实现菜单功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-for 动态生成菜单项 通过数据驱动的方式,利用 v-for 指令动态渲染菜单项。定义一个…

vue实现多级弹窗

vue实现多级弹窗

Vue 多级弹窗实现方法 方法一:使用动态组件与递归组件 在 Vue 中可以通过动态组件配合递归组件实现多级弹窗。定义一个基础弹窗组件,通过递归调用自身实现层级嵌套。 基础弹窗组件模板示例:…

vue实现前端注册

vue实现前端注册

Vue 实现前端注册功能 注册表单设计 使用 Vue 的模板语法创建注册表单,包含用户名、邮箱、密码和确认密码字段。表单需绑定 v-model 实现双向数据绑定。 <template>…

vue卖座网实现

vue卖座网实现

Vue 卖座网实现 项目结构搭建 使用 Vue CLI 快速初始化项目,安装必要依赖如 Vue Router、Vuex、Axios。 创建核心目录结构:components(通用组件)、views(页…

前端vue登录功能实现

前端vue登录功能实现

Vue 登录功能实现步骤 创建登录表单组件 在 Vue 项目中创建一个登录组件,通常命名为 Login.vue。该组件包含用户名和密码输入框以及提交按钮。 <template> &…

vue实现人脸识别比对

vue实现人脸识别比对

Vue 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…