当前位置:首页 > VUE

Vue 实现下载

2026-01-18 23:09:16VUE

Vue 实现文件下载的方法

前端下载(纯前端生成文件)

适用于需要动态生成文件内容并下载的场景,如导出表格数据为 CSV 或 Excel。

// 创建 Blob 对象并触发下载
const downloadFile = (content, fileName, type = 'text/plain') => {
  const blob = new Blob([content], { type })
  const url = URL.createObjectURL(blob)
  const link = document.createElement('a')
  link.href = url
  link.download = fileName
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
  URL.revokeObjectURL(url)
}

// 使用示例(导出CSV)
const exportCSV = () => {
  const csvContent = '姓名,年龄\n张三,25\n李四,30'
  downloadFile(csvContent, '用户数据.csv', 'text/csv')
}

后端接口下载

适用于从服务器获取文件进行下载的场景,如下载服务器上的 PDF 或图片。

// 通过接口下载文件
const downloadFromServer = (url, fileName) => {
  fetch(url)
    .then(response => response.blob())
    .then(blob => {
      const downloadUrl = URL.createObjectURL(blob)
      const a = document.createElement('a')
      a.href = downloadUrl
      a.download = fileName
      document.body.appendChild(a)
      a.click()
      document.body.removeChild(a)
      URL.revokeObjectURL(downloadUrl)
    })
}

// 使用示例
downloadFromServer('/api/download/file.pdf', '文档.pdf')

使用 axios 下载文件

当需要处理更复杂的请求(如带认证头)时,可以使用 axios。

import axios from 'axios'

const downloadWithAxios = (url, fileName) => {
  axios({
    url,
    method: 'GET',
    responseType: 'blob',
    headers: { Authorization: 'Bearer your_token' }
  }).then(response => {
    const downloadUrl = URL.createObjectURL(new Blob([response.data]))
    const link = document.createElement('a')
    link.href = downloadUrl
    link.download = fileName
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
    URL.revokeObjectURL(downloadUrl)
  })
}

处理大文件下载

对于大文件下载,可以显示进度条。

const downloadLargeFile = (url, fileName) => {
  axios({
    url,
    method: 'GET',
    responseType: 'blob',
    onDownloadProgress: progressEvent => {
      const percent = Math.round((progressEvent.loaded * 100) / progressEvent.total)
      console.log(`下载进度: ${percent}%`)
      // 可以更新 Vue 组件中的进度状态
    }
  }).then(response => {
    // 同上处理下载
  })
}

注意事项

Vue 实现下载

  • 跨域问题需要后端配置 CORS
  • 移动端可能需要特殊处理
  • 某些浏览器可能会拦截弹出式下载窗口
  • 对于敏感文件,建议通过后端验证权限

以上方法可以根据实际需求组合使用,前端生成适合简单数据导出,后端下载适合已有文件的场景。

标签: Vue
分享给朋友:

相关文章

Vue实现ping

Vue实现ping

Vue 实现 Ping 功能的方法 在 Vue 中实现 Ping 功能通常需要结合前端和后端技术。以下是几种常见的方法: 使用 JavaScript 的 fetch API 或 axios 通过发送…

Vue前端路由实现

Vue前端路由实现

Vue前端路由的实现方式 Vue前端路由通常通过vue-router库实现,它是Vue.js官方的路由管理器,用于构建单页面应用(SPA)。以下是核心实现方法和步骤: 安装vue-router 通过…

Vue实现位置切换

Vue实现位置切换

Vue实现元素位置切换的方法 在Vue中实现元素位置切换可以通过多种方式实现,以下列出几种常见方法: 使用v-if/v-else指令 通过条件渲染切换两个元素的位置,适合简单场景: <…

Vue实现打印贴纸

Vue实现打印贴纸

Vue实现打印贴纸的方法 在Vue中实现打印贴纸功能,通常需要结合HTML模板、CSS样式和JavaScript打印API。以下是几种常见的方法: 使用window.print()方法 创建一个专…

Vue实现首次登录弹窗

Vue实现首次登录弹窗

Vue实现首次登录弹窗的方法 使用本地存储(localStorage)记录登录状态 在用户首次登录时,通过检查localStorage中是否存在特定标记来判断是否需要显示弹窗。如果标记不存在,则显示…

Vue实现多个echarts排版

Vue实现多个echarts排版

实现多个 ECharts 实例的排版布局 在 Vue 中实现多个 ECharts 实例的排版布局,可以通过 CSS 布局结合 ECharts 的响应式设计来完成。以下是几种常见方法: 使用 Flex…