当前位置:首页 > VUE

vue文件下载实现

2026-03-28 22:09:43VUE

Vue 文件下载实现方法

使用 <a> 标签下载

通过创建隐藏的 <a> 标签实现文件下载,适用于静态文件或已知 URL 的情况。

<template>
  <button @click="downloadFile">下载文件</button>
</template>

<script>
export default {
  methods: {
    downloadFile() {
      const link = document.createElement('a');
      link.href = '/path/to/file.pdf';
      link.download = 'filename.pdf';
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    }
  }
}
</script>

通过 Blob 对象下载

适用于后端返回文件流或二进制数据的场景。

vue文件下载实现

<template>
  <button @click="downloadFile">下载文件</button>
</template>

<script>
export default {
  methods: {
    async downloadFile() {
      const response = await fetch('https://api.example.com/download');
      const blob = await response.blob();
      const url = window.URL.createObjectURL(blob);

      const link = document.createElement('a');
      link.href = url;
      link.download = 'filename.pdf';
      document.body.appendChild(link);
      link.click();

      window.URL.revokeObjectURL(url);
      document.body.removeChild(link);
    }
  }
}
</script>

使用 axios 下载文件

结合 axios 处理文件下载请求,适用于需要添加认证头等复杂场景。

vue文件下载实现

<template>
  <button @click="downloadFile">下载文件</button>
</template>

<script>
import axios from 'axios';

export default {
  methods: {
    async downloadFile() {
      const response = await axios({
        url: 'https://api.example.com/download',
        method: 'GET',
        responseType: 'blob',
        headers: { Authorization: 'Bearer token' }
      });

      const url = window.URL.createObjectURL(new Blob([response.data]));
      const link = document.createElement('a');
      link.href = url;
      link.download = 'filename.pdf';
      document.body.appendChild(link);
      link.click();
      window.URL.revokeObjectURL(url);
      document.body.removeChild(link);
    }
  }
}
</script>

处理大文件下载

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

<template>
  <button @click="downloadLargeFile">下载大文件</button>
  <div v-if="progress > 0">下载进度: {{ progress }}%</div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      progress: 0
    };
  },
  methods: {
    async downloadLargeFile() {
      const response = await axios({
        url: 'https://api.example.com/large-file',
        method: 'GET',
        responseType: 'blob',
        onDownloadProgress: (progressEvent) => {
          this.progress = Math.round(
            (progressEvent.loaded * 100) / progressEvent.total
          );
        }
      });

      const url = window.URL.createObjectURL(new Blob([response.data]));
      const link = document.createElement('a');
      link.href = url;
      link.download = 'large-file.zip';
      document.body.appendChild(link);
      link.click();
      window.URL.revokeObjectURL(url);
      document.body.removeChild(link);
      this.progress = 0;
    }
  }
}
</script>

服务端渲染(SSR)兼容方案

在 Nuxt.js 等 SSR 框架中,需要判断运行环境。

if (process.client) {
  const link = document.createElement('a');
  link.href = url;
  link.download = 'file.pdf';
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}

注意事项

  • 跨域问题需要后端配置 CORS
  • 大文件下载建议分块处理
  • 移动端可能需要特殊处理
  • 文件名编码问题需要注意
  • 权限控制需在后端实现

以上方法覆盖了 Vue 中文件下载的常见场景,可根据具体需求选择合适方案。

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

相关文章

vue实现逻辑

vue实现逻辑

Vue 实现逻辑的核心概念 Vue.js 是一个渐进式 JavaScript 框架,其核心逻辑围绕数据驱动和组件化开发。通过响应式系统、虚拟 DOM 和组件生命周期等机制,Vue 实现了高效的前端开发…

vue实现mui

vue实现mui

使用 Vue 实现 MUI(Material-UI)风格的界面 Vue 本身不直接提供 MUI(Material Design)组件,但可以通过集成第三方库或手动实现来达到类似效果。以下是几种常见方法…

vue 实现hover

vue 实现hover

Vue 实现 Hover 效果的方法 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 伪类 :hover 最简单的方式是直接使用 CSS 的 :hov…

vue实现popover

vue实现popover

Vue 实现 Popover 的方法 使用第三方库(如 Element UI、Ant Design Vue) 许多成熟的 UI 库已经内置了 Popover 组件,可以直接调用。 Element U…

vue ui实现创建vue项目

vue ui实现创建vue项目

使用 Vue UI 创建 Vue 项目 Vue UI 是 Vue CLI 提供的图形化界面工具,可以通过可视化操作创建和管理 Vue 项目。 安装 Vue CLI 确保已安装 Node.js(建议版…

vue实现菜单定位

vue实现菜单定位

实现菜单定位的方法 在Vue中实现菜单定位功能,可以通过监听滚动事件或使用Intersection Observer API来判断当前显示的菜单项,并高亮对应的导航链接。以下是几种常见的实现方式:…