当前位置:首页 > 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 实现好评弹框可以通过组件化方式完成,结合动态数据绑定和事件处理。以下是具体实现步骤: 1. 创建弹框组件 新建一个 RatingDialog.vue 组件…

vue怎么实现选中删除

vue怎么实现选中删除

实现选中删除功能的方法 在Vue中实现选中删除功能通常涉及以下几个关键步骤: 数据绑定与选中状态管理 使用v-model或v-for指令绑定列表数据,结合复选框或单选按钮实现选中状态管理。例如:…

用vue实现搜索查询

用vue实现搜索查询

实现搜索查询的基本思路 在Vue中实现搜索查询功能通常涉及以下几个核心部分:数据绑定、事件监听、过滤逻辑和UI反馈。以下是一个完整的实现方案。 数据绑定与输入框 创建一个Vue组件,包含一个输入框用…

vue实现图库

vue实现图库

Vue 实现图库的基本方法 使用 Vue 实现图库功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用 Vue 和第三方库(如 Vue Gallery) 安装 vue-gallery 或…

vue实现图集

vue实现图集

Vue 实现图集的方法 在 Vue 中实现图集功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用第三方库(如 vue-image-lightbox) 安装 vue-image-ligh…

vue实现表白

vue实现表白

Vue 实现表白页面 使用 Vue 可以快速创建一个动态、交互式的表白页面。以下是一个简单的实现方案: 基础结构 创建一个 Vue 项目或单文件组件,包含以下核心部分: <template&…