当前位置:首页 > 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 中实现页签功能通常涉及动态组件、路由或状态管理。以下是几种常见的实现方式: 使用动态组件 通过 Vue 的 <component> 动态组件结合 v-f…

vue怎么实现文件树

vue怎么实现文件树

Vue 实现文件树的方法 使用递归组件 递归组件是处理嵌套数据结构的理想方式。文件树通常包含文件夹和文件的嵌套结构,适合用递归组件实现。 <template> <ul>…

js实现vue

js实现vue

Vue.js 的基本实现方法 在 JavaScript 中实现 Vue.js 的功能,可以通过直接引入 Vue 库或使用现代构建工具(如 Vite 或 Webpack)。以下是几种常见的实现方式:…

vue交互实现

vue交互实现

Vue 交互实现方法 Vue.js 提供了多种方式实现用户交互,包括事件处理、表单绑定、动态渲染等。以下是常见的交互实现方法: 事件处理 通过 v-on 或 @ 指令绑定事件,触发方法或直接执行表达…

vue 实现全屏

vue 实现全屏

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

vue实现123456

vue实现123456

Vue 实现数字 123456 的显示 在 Vue 中显示数字 123456 非常简单,可以通过以下方法实现: 方法一:直接在模板中显示 <template> <div>…