当前位置:首页 > VUE

vue axios实现文件下载

2026-01-20 12:43:05VUE

使用 Axios 实现文件下载

在 Vue 项目中,可以通过 Axios 发送请求实现文件下载功能。以下是具体实现方法:

发送 GET 请求下载文件

axios({
  method: 'get',
  url: '/api/download',
  responseType: 'blob'
}).then(response => {
  const url = window.URL.createObjectURL(new Blob([response.data]))
  const link = document.createElement('a')
  link.href = url
  link.setAttribute('download', 'filename.ext')
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
})

发送 POST 请求下载文件

axios({
  method: 'post',
  url: '/api/download',
  data: { id: 123 },
  responseType: 'blob'
}).then(response => {
  const url = window.URL.createObjectURL(new Blob([response.data]))
  const link = document.createElement('a')
  link.href = url
  link.setAttribute('download', 'filename.ext')
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
})

处理后端返回的文件名

如果后端在响应头中返回文件名,可以这样处理:

axios({
  method: 'get',
  url: '/api/download',
  responseType: 'blob'
}).then(response => {
  const contentDisposition = response.headers['content-disposition']
  let filename = 'default.ext'
  if (contentDisposition) {
    const match = contentDisposition.match(/filename="?(.+)"?/)
    if (match && match[1]) {
      filename = match[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)
})

封装为可复用方法

可以将文件下载功能封装为可复用的方法:

vue axios实现文件下载

// utils/download.js
export function downloadFile(url, params = {}, method = 'get', filename = '') {
  return axios({
    method,
    url,
    data: method === 'post' ? params : null,
    params: method === 'get' ? params : null,
    responseType: 'blob'
  }).then(response => {
    const contentDisposition = response.headers['content-disposition']
    let finalFilename = filename

    if (!finalFilename && contentDisposition) {
      const match = contentDisposition.match(/filename="?(.+)"?/)
      if (match && match[1]) {
        finalFilename = match[1]
      }
    }

    const blobUrl = window.URL.createObjectURL(new Blob([response.data]))
    const link = document.createElement('a')
    link.href = blobUrl
    link.setAttribute('download', finalFilename || 'file')
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
    window.URL.revokeObjectURL(blobUrl)
  })
}

在组件中使用

import { downloadFile } from '@/utils/download'

// 调用示例
downloadFile('/api/download', { id: 123 }, 'get', 'custom-filename.ext')

注意事项

  1. 确保后端接口正确设置了响应头 Content-Disposition
  2. 对于大文件下载,可以考虑添加进度提示
  3. 在下载完成后调用 window.URL.revokeObjectURL() 释放内存
  4. 某些浏览器可能需要用户交互才能触发下载

标签: 文件vue
分享给朋友:

相关文章

vue搜索过后实现分页

vue搜索过后实现分页

Vue 实现搜索后分页功能 数据绑定与搜索逻辑 在 Vue 组件中定义必要的数据属性: data() { return { searchQuery: '', currentPag…

vue穿梭框组件实现

vue穿梭框组件实现

实现基础穿梭框组件 在Vue中实现穿梭框组件,可以使用<el-transfer>(Element UI)或手动实现。以下是手动实现的核心逻辑: <template> &l…

vue实现阻塞

vue实现阻塞

Vue 实现阻塞的方法 在 Vue 中实现阻塞操作通常涉及异步控制、状态管理或生命周期钩子的使用。以下是几种常见方法: 使用 async/await 处理异步阻塞 通过 async/await 可…

vue实现树形

vue实现树形

Vue 实现树形组件 在 Vue 中实现树形结构通常需要递归组件和动态数据绑定。以下是实现树形组件的几种常见方法。 递归组件实现 递归组件是 Vue 中实现树形结构的核心方法。通过组件调用自身,可以…

vue博客实现

vue博客实现

Vue 博客实现步骤 项目初始化与基础配置 使用 Vue CLI 或 Vite 创建项目,安装基础依赖如 vue-router 和状态管理库(如 pinia)。配置路由文件实现页面跳转逻辑,例如博客首…

vue实现导出excel实现流程

vue实现导出excel实现流程

安装依赖库 需要安装 xlsx 和 file-saver 库来处理 Excel 文件的生成和保存。通过 npm 或 yarn 安装: npm install xlsx file-saver # 或…