当前位置:首页 > 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 中,通过模板或计算属性实时显示进度条。

vue前端实现下载进度

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)的进度条组件,实现可视化效果。

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

vue实现放大效果

vue实现放大效果

使用 CSS transform 实现放大效果 在 Vue 中实现放大效果可以通过 CSS 的 transform: scale() 属性。这种方法简单高效,适合大多数交互场景。 定义一个 CSS…

vue实现按钮组轮换

vue实现按钮组轮换

实现按钮组轮换的方法 在Vue中实现按钮组轮换效果可以通过动态绑定类和事件处理来完成。以下是一种常见的实现方式: 模板部分 <template> <div class="…

vue前端分页怎么实现

vue前端分页怎么实现

前端分页实现方法 在Vue中实现前端分页通常涉及数据分割和页面控制逻辑。以下是常见的实现方式: 使用计算属性分页 通过计算属性对数据进行切片处理: computed: { paginatedD…

vue实现活动倒计时

vue实现活动倒计时

实现活动倒计时的基本思路 在Vue中实现活动倒计时通常需要获取当前时间与活动结束时间的差值,并将其转换为天、小时、分钟、秒的格式展示。通过定时器每秒更新显示的时间。 定义倒计时数据 在Vue组件的d…

vue实现双折线图

vue实现双折线图

实现双折线图的步骤 安装必要的依赖库(如 ECharts 或 Chart.js),这里以 ECharts 为例: npm install echarts --save 在 Vue 组件中引入 EC…