当前位置: 首页 > news >正文

vue3 文件下载(excel/rar/zip)

安装axios

npm install axios

在项目中引入

 

import axios from 'axios';

1、get接口excel文件下载


const file_key = ref('')
const downLoadExcel = (value:any) => {//file_key.value = value axios({method: "get",url: "/api/da/download_excel/",//url: "/api/da/download_excel/" + file_key.value + "/",//带参数params: params.value,responseType: "blob",headers: {Authorization: "Bearer "+sessionStorage.getItem("token"),'Cache-Control': 'no-cache','Pragma': 'no-cache',},}).then((res) => {const blob = new Blob([res.data]);let contentDisposition = res.headers["content-disposition"];// let fileName = window.decodeURI(//   contentDisposition.substring(contentDisposition.indexOf("=") + 1)// );let fileName = 'downLoad.xlsx';const elink = document.createElement("a"); // 创建a标签elink.download = fileName; // 为a标签添加download属性elink.style.display = "none";elink.href = URL.createObjectURL(blob);document.body.appendChild(elink);elink.click(); // 点击下载URL.revokeObjectURL(elink.href); // 释放URL 对象document.body.removeChild(elink); // 释放标签message.success('下载成功')}).catch((error) => {message.error('下载失败')});
}

2、get 下载 rar 文件

const downLoadRar = () => {axios({method: "get",url: "/api/dl/downloadmodel/",responseType: "blob",headers: {Authorization: "Bearer "+sessionStorage.getItem("token"),},}).then((res) => {const blob = new Blob([res.data], { type: 'application/x-rar-compressed' });let contentDisposition = res.headers["content-disposition"];let fileName = '下载文件.rar';if (contentDisposition) { // 如果响应头中包含文件名信息,则解析出来const regex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;const matches = regex.exec(contentDisposition);if (matches != null && matches[1]) {fileName = matches[1].replace(/['"]/g, '');}}const elink = document.createElement("a"); // 创建a标签elink.download = fileName; // 为a标签添加download属性elink.style.display = "none";elink.href = URL.createObjectURL(blob);document.body.appendChild(elink);elink.click(); // 点击下载URL.revokeObjectURL(elink.href); // 释放URL 对象document.body.removeChild(elink);message.success('导出成功')}).catch((error) => {message.error('导出失败')});
}

3、get下载 zip 文件

const downLoadZip = () => {axios({method: "get",url: "/api/download_file/",responseType: "blob",headers: {Authorization: "Bearer "+sessionStorage.getItem("token"),},}).then((res) => {const blob = new Blob([res.data], { type: 'application/zip' });let contentDisposition = res.headers["content-disposition"];let fileName = 'downloaded_file.zip'; // 默认文件名// 尝试从响应头中提取文件名,假设文件名包含中文或特殊字符时使用了UTF-8编码if (contentDisposition && contentDisposition.indexOf('filename') !== -1) {const regex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;const matches = regex.exec(contentDisposition);if (matches != null && matches[1]) {fileName = decodeURIComponent(matches[1].trim().replace(/['"]/g, ''));}}const elink = document.createElement("a"); // 创建a标签elink.download = fileName; // 为a标签添加download属性elink.style.display = "none";elink.href = URL.createObjectURL(blob);document.body.appendChild(elink);elink.click(); // 点击下载URL.revokeObjectURL(elink.href); // 释放URL 对象document.body.removeChild(elink); // 释放标签message.success('导出成功')}).catch((error) => {message.error('导出失败')});
}

post 下载

const downLoad = () => {axios({method: "post",url: "/api/download_file/",responseType: "blob",headers: {Authorization: "Bearer "+sessionStorage.getItem("token"),},//post 参数data: {file_key: data.filename}}).then((res) => {//下载zipconst blob = new Blob([res.data], { type: 'application/zip' });//下载rar//const blob = new Blob([res.data], { type: 'application/x-rar-compressed' });//下载excel// const blob = new Blob([res.data]);let contentDisposition = res.headers["content-disposition"];let fileName = 'downloaded_file.zip'; // 默认文件名// 尝试从响应头中提取文件名,假设文件名包含中文或特殊字符时使用了UTF-8编码if (contentDisposition && contentDisposition.indexOf('filename') !== -1) {const regex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;const matches = regex.exec(contentDisposition);if (matches != null && matches[1]) {fileName = decodeURIComponent(matches[1].trim().replace(/['"]/g, ''));}}const elink = document.createElement("a"); // 创建a标签elink.download = fileName; // 为a标签添加download属性elink.style.display = "none";elink.href = URL.createObjectURL(blob);document.body.appendChild(elink);elink.click(); // 点击下载URL.revokeObjectURL(elink.href); // 释放URL 对象document.body.removeChild(elink); // 释放标签message.success('导出成功')}).catch((error) => {message.error('导出失败')});
}

相关文章:

  • SQL Server中OPENJSON + WITH 来解析JSON
  • unity使用内建组件给刚体增加重力
  • 基于YOLO11的车牌识别分析系统
  • Windows VsCode Terminal窗口使用Linux命令
  • 【Vue】从 MVC 到 MVVM:前端架构演变与 Vue 的实践之路
  • Docker Search 和 Docker Pull 失效解决
  • C# WinForm窗口TextBox控件只能输入数字(包括小数)并且恢复Ctrl+C复制和Ctrl+V粘贴功能
  • 矩阵游戏--二分图的匈牙利算法
  • Vulkanised
  • 隧道消防报警综合盘使用说明书及详细介绍
  • React 入门完全指南:从零开始构建现代 Web 应用
  • 【无标题】spark SQL核心编程
  • STL详解 - list的模拟实现
  • C++零基础实践教程 指针与内存 类与对象入门 (面向对象基础)
  • 第五节:React Hooks进阶篇-如何用useMemo/useCallback优化性能
  • eSIM RSP(远程SIM配置)架构笔记
  • Spring Boot整合T-IO实现即时通讯
  • 记录第一次面试的经历
  • 游戏盾是什么?重新定义游戏安全边界
  • Sklearn入门之数据预处理preprocessing
  • 佛山网站排名推广/seoapp推广
  • 用dw做网站的视频/关键词优化是怎么做的
  • docker安装wordpress/seo需要什么技术
  • 用cms创建自己带数据库的网站/百度快照替代
  • 上海市建设委员会的网站查询系统/百度收录的网页数量
  • html5+css3网站模板/seo课程培训要多少钱