当前位置:首页 > VUE

vue实现get下载

2026-03-08 15:41:03VUE

使用 Vue 实现 GET 下载

方法一:通过 window.open 直接下载

直接通过 window.open 打开下载链接,适用于简单的文件下载场景。

downloadFile() {
  const url = 'https://example.com/file.pdf';
  window.open(url, '_blank');
}

方法二:使用 axios 发送 GET 请求

通过 axios 发送 GET 请求获取文件流,并使用 Blob 对象创建下载链接。

import axios from 'axios';

downloadFile() {
  axios({
    url: 'https://example.com/file.pdf',
    method: 'GET',
    responseType: 'blob',
  }).then(response => {
    const url = window.URL.createObjectURL(new Blob([response.data]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', 'file.pdf');
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  });
}

方法三:处理带认证的下载

如果下载接口需要认证(如 Token),可以在请求头中添加认证信息。

downloadFileWithAuth() {
  const token = localStorage.getItem('token');
  axios({
    url: 'https://example.com/protected-file.pdf',
    method: 'GET',
    responseType: 'blob',
    headers: {
      'Authorization': `Bearer ${token}`
    }
  }).then(response => {
    const url = window.URL.createObjectURL(new Blob([response.data]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', 'protected-file.pdf');
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  });
}

方法四:处理后端返回的文件名

如果后端在响应头中返回文件名,可以通过解析 content-disposition 获取。

downloadFileWithDynamicName() {
  axios({
    url: 'https://example.com/dynamic-file',
    method: 'GET',
    responseType: 'blob',
  }).then(response => {
    const contentDisposition = response.headers['content-disposition'];
    const fileName = contentDisposition.split('filename=')[1];
    const url = window.URL.createObjectURL(new Blob([response.data]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', fileName);
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  });
}

方法五:通过 fetch 实现下载

如果不使用 axios,可以通过原生 fetch 实现下载。

vue实现get下载

downloadWithFetch() {
  fetch('https://example.com/file.pdf')
    .then(response => response.blob())
    .then(blob => {
      const url = window.URL.createObjectURL(blob);
      const link = document.createElement('a');
      link.href = url;
      link.setAttribute('download', 'file.pdf');
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    });
}

注意事项

  • 确保后端接口支持跨域请求(CORS),否则下载可能失败。
  • 如果文件较大,可以添加加载提示以改善用户体验。
  • 下载完成后,记得释放 Blob URL 以避免内存泄漏。
// 释放 Blob URL
window.URL.revokeObjectURL(url);

以上方法涵盖了 Vue 中实现 GET 下载的常见场景,可以根据实际需求选择合适的方式。

标签: vueget
分享给朋友:

相关文章

vue设计与实现下载

vue设计与实现下载

vue设计与实现电子书下载 《Vue.js设计与实现》是一本深入解析Vue.js框架原理的书籍,由霍春阳(HcySunYang)撰写。以下是获取该资源的常见途径: 正版购买渠道 京东、当当、天猫等…

vue实现日历

vue实现日历

Vue 实现日历的基本方法 使用 Vue 实现日历可以通过自定义组件或借助第三方库完成。以下是两种常见实现方式: 自定义日历组件 创建基于 Vue 的日历组件需要处理日期计算和渲染逻辑。核心是生成当…

vue实现队列

vue实现队列

Vue 实现队列功能 在 Vue 中实现队列功能可以通过多种方式完成,以下是几种常见的方法: 使用数组模拟队列 队列遵循先进先出(FIFO)原则,可以用数组的 push 和 shift 方法模拟入队…

vue实现按钮

vue实现按钮

Vue 实现按钮的方法 使用原生 HTML 按钮 在 Vue 模板中可以直接使用 HTML 的 <button> 元素,通过 v-on 或 @ 绑定点击事件。 <template&…

vue实现登录权限

vue实现登录权限

Vue 实现登录权限方案 路由守卫控制访问权限 安装vue-router并配置路由守卫,在全局前置守卫中检查用户登录状态和权限。未登录用户访问受限路由时重定向到登录页。 // router.js r…

vue实现条件搜索

vue实现条件搜索

实现条件搜索的基本思路 在Vue中实现条件搜索通常涉及以下几个关键步骤:绑定表单输入、处理搜索逻辑、动态过滤数据。以下是具体实现方法。 表单数据绑定 使用v-model双向绑定搜索条件表单元素,将用…