当前位置:首页 > VUE

vue实现下载暂停

2026-01-12 05:12:31VUE

Vue 实现下载暂停功能

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

使用 AbortController 中断请求

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

// 在 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;
    }
  }
}

实现断点续传

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

vue实现下载暂停

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中实现自定义登录功能通常需要结合前端和后端技术。以下是一个基本的实现流程: 创建登录表单组件 使用Vue的单文件组件创建一个登录表单,包含用户名和密码输入框以及提交…

vue实现排序

vue实现排序

Vue 实现排序的方法 在 Vue 中实现排序可以通过多种方式完成,以下是一些常见的方法: 使用计算属性排序数组 计算属性非常适合对数据进行排序,因为它会在依赖的数据变化时自动更新。以下是一个示例:…

vue实现图册

vue实现图册

Vue实现图册功能 在Vue中实现图册功能通常涉及图片展示、切换、缩放等交互效果。以下是常见的实现方法和关键代码示例: 基础图册组件结构 使用Vue单文件组件构建基础结构: <templat…

vue实现复选

vue实现复选

Vue 实现复选框 在 Vue 中实现复选框可以通过 v-model 指令绑定数据,同时结合 input 元素的 type="checkbox" 属性来实现。以下是几种常见的实现方式: 单个复选框…

vue实现数组

vue实现数组

Vue 实现数组操作的方法 在 Vue 中,数组的操作需要特别注意响应式的问题。Vue 提供了一些方法来确保数组的变动能够触发视图更新。 响应式数组方法 Vue 对以下数组方法进行了封装,确保它们能…

vue 实现全屏

vue 实现全屏

Vue 实现全屏功能 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 完成。以下是一个完整的实现方案: 安装依赖(可选) 如果需要跨浏览器兼容性更好,可以安装…