当前位置:首页 > 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使用vr实现标注

vue使用vr实现标注

Vue 中使用 VR 实现标注的方法 在 Vue 项目中结合 VR 技术实现标注功能,可以通过 WebXR 和 Three.js 等库实现。以下为具体实现方法: 安装依赖 确保项目中已安装 Thre…

vue实现muli

vue实现muli

Vue 实现多语言(i18n)支持 在 Vue 项目中实现多语言(i18n)功能,通常使用 vue-i18n 插件。以下是具体实现步骤: 安装 vue-i18n npm install vue-i…

vue mvvm实现

vue mvvm实现

Vue MVVM 实现原理 Vue.js 是一个基于 MVVM(Model-View-ViewModel)模式的框架,其核心是通过数据绑定和响应式系统实现视图与数据的自动同步。以下是 Vue 实现 M…

vue实现监听

vue实现监听

监听数据变化 在Vue中,可以通过watch选项或$watch方法监听数据的变化。watch适用于组件选项内声明式监听,$watch适用于动态监听。 // 选项式API export default…

vue实现加减

vue实现加减

Vue 实现加减功能 在 Vue 中实现加减功能可以通过数据绑定和事件监听来完成。以下是一个简单的实现方法: 模板部分 <template> <div> <…

vue实现标注

vue实现标注

Vue 实现标注功能的方法 使用 Vue 实现标注功能可以通过多种方式实现,以下介绍几种常见的方法: 1. 使用 HTML5 Canvas 实现标注 Canvas 提供了强大的绘图能力,适合实现复…