当前位置:首页 > 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 文件上传实现方法 使用原生 HTML 文件输入元素 在 Vue 组件中可以直接使用 HTML 的原生文件上传控件,通过 v-on 监听 change 事件获取文件对象。 <templa…

vue实现视频开场

vue实现视频开场

使用 Vue 实现视频开场动画 在 Vue 中实现视频开场动画可以通过结合 HTML5 的 <video> 标签和 Vue 的过渡效果或生命周期钩子来完成。以下是具体实现方法: 安装依…

vue实现多层评论回复

vue实现多层评论回复

Vue 实现多层评论回复 数据结构设计 多层评论通常采用嵌套结构,每个评论对象包含子评论数组。示例数据结构如下: comments: [ { id: 1, content: '…

vue实现查询替换

vue实现查询替换

Vue 实现查询替换功能 在 Vue 中实现查询替换功能,可以通过数据绑定和字符串操作方法结合实现。以下是具体实现方式: 基础实现 <template> <div>…

vue按钮实现截屏

vue按钮实现截屏

Vue 按钮实现截屏的方法 在 Vue 项目中实现截屏功能可以通过多种方式完成,以下是几种常见的方法: 使用 html2canvas 库 安装 html2canvas 库: npm ins…

vue穿梭框组件实现

vue穿梭框组件实现

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