当前位置:首页 > 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 对象下载

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

<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 处理文件下载请求,适用于需要添加认证头等复杂场景。

<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 框架中,需要判断运行环境。

vue文件下载实现

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 router 实现

vue router 实现

Vue Router 的实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的核心实现方式。 安装 Vue Router 通过…

vue 实现流程

vue 实现流程

Vue 实现流程 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是 Vue 实现的基本流程: 安装 Vue.js 通过 CDN 引入或使用 npm/yarn 安装:…

vue实现slidetoggle

vue实现slidetoggle

Vue 实现 SlideToggle 效果 SlideToggle 是一种常见的交互效果,元素以滑动方式展开或收起。以下是几种实现方法: 使用 CSS Transition 和 v-show…

vue实现layout

vue实现layout

Vue 实现 Layout 布局的方法 在 Vue 中实现 Layout 布局通常涉及路由嵌套、组件化设计和动态渲染。以下是几种常见的实现方式: 使用嵌套路由 通过 Vue Router 的嵌套路由…

vue实现定位打卡

vue实现定位打卡

Vue 实现定位打卡功能 获取用户地理位置 使用浏览器提供的 Geolocation API 获取用户当前位置坐标。在 Vue 组件中可以通过 navigator.geolocation 调用。 m…

vue 实现异步加载

vue 实现异步加载

Vue 实现异步加载的方法 使用动态 import 实现组件懒加载 在 Vue 中可以通过动态 import() 语法实现组件的异步加载,这会使得组件在需要时才被加载,减少初始加载时间。 const…