当前位置:首页 > VUE

vue实现下载暂停

2026-01-07 04:41:26VUE

Vue实现下载暂停功能

在Vue中实现下载暂停功能,通常需要结合XMLHttpRequest或Fetch API的AbortController来控制请求中断。以下是具体实现方法:

使用XMLHttpRequest实现

创建XMLHttpRequest对象并存储其引用,便于后续操作:

data() {
  return {
    xhr: null,
    downloadProgress: 0,
    isPaused: false
  }
}

发起下载请求时保存xhr对象:

downloadFile() {
  this.xhr = new XMLHttpRequest();
  this.xhr.open('GET', 'file-url', true);
  this.xhr.responseType = 'blob';

  this.xhr.onprogress = (event) => {
    if (event.lengthComputable) {
      this.downloadProgress = Math.round((event.loaded / event.total) * 100);
    }
  };

  this.xhr.onload = () => {
    if (this.xhr.status === 200) {
      const blob = new Blob([this.xhr.response]);
      const link = document.createElement('a');
      link.href = URL.createObjectURL(blob);
      link.download = 'filename.ext';
      link.click();
    }
  };

  this.xhr.send();
}

暂停下载时调用abort方法:

pauseDownload() {
  if (this.xhr) {
    this.xhr.abort();
    this.isPaused = true;
  }
}

使用Fetch API实现

结合AbortController实现更现代的暂停控制:

data() {
  return {
    controller: null,
    downloadProgress: 0,
    isPaused: false
  }
}

发起请求时创建AbortController:

async downloadFile() {
  this.controller = new AbortController();
  try {
    const response = await fetch('file-url', {
      signal: this.controller.signal
    });

    const reader = response.body.getReader();
    const contentLength = +response.headers.get('Content-Length');
    let receivedLength = 0;
    let chunks = [];

    while(true) {
      const {done, value} = await reader.read();
      if (done) break;

      chunks.push(value);
      receivedLength += value.length;
      this.downloadProgress = Math.round((receivedLength / contentLength) * 100);
    }

    const blob = new Blob(chunks);
    const link = document.createElement('a');
    link.href = URL.createObjectURL(blob);
    link.download = 'filename.ext';
    link.click();
  } catch (err) {
    if (err.name === 'AbortError') {
      console.log('Download aborted');
    }
  }
}

暂停时调用abort方法:

pauseDownload() {
  if (this.controller) {
    this.controller.abort();
    this.isPaused = true;
  }
}

实现断点续传

对于大文件下载,可考虑实现断点续传:

resumeDownload() {
  const rangeHeader = `bytes=${this.downloadedBytes}-`;
  fetch('file-url', {
    headers: { 'Range': rangeHeader },
    signal: this.controller.signal
  });
  // 处理后续分块下载逻辑
}

注意服务端需要支持Range请求头,并正确处理206 Partial Content响应。

UI状态管理

在模板中绑定下载状态:

<button @click="downloadFile" :disabled="!!xhr && !isPaused">
  {{ isPaused ? 'Resume' : 'Download' }}
</button>
<button @click="pauseDownload" :disabled="!xhr || isPaused">
  Pause
</button>
<progress :value="downloadProgress" max="100"></progress>

这种方法提供了基本的下载控制功能,可根据实际需求扩展更复杂的逻辑。

vue实现下载暂停

标签: vue
分享给朋友:

相关文章

vue实现拖拽

vue实现拖拽

Vue 实现拖拽功能 在 Vue 中实现拖拽功能可以通过原生 HTML5 的拖拽 API 或第三方库如 vuedraggable 来实现。以下是两种常见方法的详细说明: 使用 HTML5 拖拽 A…

vue实现组件循环

vue实现组件循环

Vue 组件循环的实现方法 在 Vue 中实现组件循环通常使用 v-for 指令,可以循环渲染数组或对象的数据到组件上。 基本数组循环 通过 v-for 指令遍历数组,渲染多个相同结构的组件。…

vue实现录播播放

vue实现录播播放

Vue 实现录播播放 使用 video.js 实现 安装 video.js 和相关的 Vue 适配器: npm install video.js @videojs-player/vue 在 Vue…

vue卖座网实现

vue卖座网实现

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

vue实现好友列表

vue实现好友列表

实现好友列表的Vue组件 数据准备 在Vue组件的data中定义好友列表数据,通常是一个数组对象,每个对象包含好友的基本信息如ID、头像、昵称等。 data() { return {…

vue登录逻辑的实现

vue登录逻辑的实现

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