当前位置:首页 > VUE

vue实现下载暂停

2026-01-12 05:12:31VUE

Vue 实现下载暂停功能

在 Vue 中实现下载暂停功能需要结合前端技术如 fetchXMLHttpRequest 进行控制,以下是具体实现方法:

vue实现下载暂停

使用 AbortController 中断请求

通过 AbortController 可以中断正在进行的下载请求,实现暂停效果。

vue实现下载暂停

// 在 Vue 组件中
data() {
  return {
    controller: null,
    isDownloading: false,
    downloadProgress: 0
  };
},
methods: {
  startDownload() {
    this.controller = new AbortController();
    this.isDownloading = true;
    fetch('your-download-url', {
      signal: this.controller.signal
    })
    .then(response => {
      const reader = response.body.getReader();
      const contentLength = +response.headers.get('Content-Length');
      let receivedLength = 0;

      const processChunk = ({ done, value }) => {
        if (done) {
          this.isDownloading = false;
          return;
        }
        receivedLength += value.length;
        this.downloadProgress = (receivedLength / contentLength) * 100;
        return reader.read().then(processChunk);
      };
      return reader.read().then(processChunk);
    })
    .catch(err => {
      if (err.name === 'AbortError') {
        console.log('Download aborted');
      }
    });
  },
  pauseDownload() {
    if (this.controller) {
      this.controller.abort();
      this.isDownloading = false;
    }
  }
}

使用 XMLHttpRequest 实现暂停和恢复

XMLHttpRequest 可以获取下载进度并实现更细粒度的控制。

data() {
  return {
    xhr: null,
    downloadedBytes: 0,
    totalBytes: 0,
    isPaused: false
  };
},
methods: {
  startDownload() {
    this.xhr = new XMLHttpRequest();
    this.xhr.open('GET', 'your-download-url', true);
    this.xhr.responseType = 'blob';

    this.xhr.onprogress = (event) => {
      if (event.lengthComputable) {
        this.downloadedBytes = event.loaded;
        this.totalBytes = event.total;
      }
    };

    this.xhr.onload = () => {
      if (this.xhr.status === 200) {
        const blob = this.xhr.response;
        // 处理下载完成的 blob
      }
    };

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

实现断点续传

对于大文件下载,可以结合服务器支持实现断点续传。

resumeDownload() {
  this.xhr = new XMLHttpRequest();
  this.xhr.open('GET', 'your-download-url', true);
  this.xhr.setRequestHeader('Range', `bytes=${this.downloadedBytes}-`);
  this.xhr.responseType = 'blob';

  this.xhr.onprogress = (event) => {
    // 更新进度
  };

  this.xhr.send();
}

注意事项

  • 服务器需要支持 Range 请求头才能实现断点续传。
  • 下载进度显示需要确保响应头包含 Content-Length
  • 中断下载后可能需要清理已下载的临时数据。

通过以上方法可以在 Vue 应用中实现下载暂停功能,根据具体需求选择适合的方案。

标签: vue
分享给朋友:

相关文章

vue实现文章锚点定位

vue实现文章锚点定位

实现文章锚点定位的方法 在Vue中实现文章锚点定位可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML锚点 通过HTML的id属性和<a>标签的href属性实现锚点跳转。在Vu…

vue实现递归

vue实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据(如菜单、评论列表、文件目录等)。以下是具体实现方法: 定义递归组件 通过 name 选项让组件可以递归调用自身:…

vue实现翻译

vue实现翻译

Vue 实现翻译功能的方法 使用 i18n 插件实现多语言切换 安装 vue-i18n 插件: npm install vue-i18n 创建语言资源文件(如 en.json 和 zh.json):…

vue el 实现

vue el 实现

Vue 中使用 Element UI (el) 的实现方法 Element UI 是一个基于 Vue 的组件库,提供丰富的 UI 组件,常用于快速构建企业级中后台产品。以下介绍 Vue 项目中集成和使…

vue实现fadein

vue实现fadein

Vue 实现 FadeIn 效果 在 Vue 中实现淡入(FadeIn)效果可以通过 CSS 过渡、动画或第三方库实现。以下是几种常见方法: 使用 CSS 过渡 通过 Vue 的过渡系统结合 CSS…

vue 实现回复

vue 实现回复

Vue 实现回复功能 在 Vue 中实现回复功能通常涉及表单提交、数据绑定和列表渲染。以下是实现步骤和代码示例: 数据绑定与表单 <template> <div>…