当前位置:首页 > 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)
})

封装为可复用方法

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

// 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 axios实现文件下载

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

相关文章

vue怎么实现页面返回

vue怎么实现页面返回

Vue 实现页面返回的方法 在 Vue 中实现页面返回功能,可以通过以下几种方式完成,具体取决于项目使用的路由模式和技术栈。 使用 Vue Router 的编程式导航 通过 this.$router…

vue设计与实现下载

vue设计与实现下载

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

vue实现边框

vue实现边框

Vue 实现边框的方法 在 Vue 中实现边框效果可以通过多种方式完成,包括内联样式、CSS 类绑定、动态样式以及使用第三方 UI 库。以下是几种常见的实现方法。 内联样式绑定 使用 Vue 的…

vue实现星星

vue实现星星

实现星星评分的Vue组件 使用Vue实现星星评分功能可以通过组件化方式完成,以下是一个完整的实现方案: 模板部分 <template> <div class="star-ra…

vue原型实现

vue原型实现

Vue 原型实现的关键点 Vue.js 的原型系统是其响应式机制的核心部分,通过原型链实现数据的动态绑定和依赖收集。以下是实现 Vue 原型的主要技术要点。 数据响应式原理 Vue 通过 Obje…

vue实现帖子

vue实现帖子

Vue 实现帖子功能 使用 Vue 实现帖子功能需要结合前端框架和后端数据交互,以下是实现的关键步骤和代码示例。 帖子列表展示 创建帖子列表组件,使用 v-for 渲染帖子数据,通过 axios…