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

vue-axios+springboot实现文件流下载

前端vue代码:

<template><div class="app-container documentation-container"><div><el-button type="primary" @click="downloadFile('test.xlsx')">下载test.xlsx</el-button></div></div>
</template><script>
import axios from 'axios'export default {name: 'Documentation',data() {return {}},methods: {downloadFile(fileName) {axios.get('http://localhost:8081/t/downloadFile/' + fileName, {responseType: 'blob'}).then(function (response) {// 处理成功情况console.log("res:", response);if (response.headers['content-Disposition'] || response.headers['content-type'] == 'application/octet-stream') {console.log(11);let data = response.data;console.log('data', data);let blob = new Blob([data], {type: 'application/octet-stream'});let href = window.URL.createObjectURL(blob);let aElement = document.createElement('a');aElement.setAttribute('download', fileName);aElement.href = href;document.body.appendChild(aElement);aElement.click();document.body.removeChild(aElement);window.URL.revokeObjectURL(href); // 释放blob对象}}).catch(function (error) {// 处理错误情况console.log(error);});},}
}
</script>

后端代码:

@RestController
@RequestMapping("/t")
public class TestController {@CrossOrigin@RequestMapping("/downloadFile/{fileName}")public void downloadFile(HttpServletResponse response, @PathVariable("fileName") String fileName) throws IOException {String filePath = "excel/" + fileName;File file = new File(filePath);if (!file.getParentFile().exists()) {file.getParentFile().mkdir();if (!file.exists()) {file.createNewFile();}}//        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));response.addHeader(HttpHeaders.CONTENT_DISPOSITION, ContentDisposition.attachment().filename(fileName, StandardCharsets.UTF_8).build().toString());response.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);FileInputStream fileInputStream = new FileInputStream(file);ServletOutputStream outputStream = response.getOutputStream();byte[] buff = new byte[1024];int len = 0;while ((len = fileInputStream.read(buff)) != -1) {outputStream.write(buff, 0, len);}outputStream.flush();outputStream.close();fileInputStream.close();}
}
http://www.lryc.cn/news/510980.html

相关文章:

  • selenium执行js
  • 每日算法Day11【左叶子之和、找树左下角的值、路径总和】
  • 分享一下使用 AI 开发个人工具的迭代过程
  • 大型语言模型(LLMs)演化树 Large Language Models
  • 部分背包问题
  • 教师管理系统
  • Word论文交叉引用一键上标
  • 集成方案 | Docusign + 蓝凌 EKP,打造一站式合同管理平台,实现无缝协作!
  • Python大数据可视化:基于python大数据的电脑硬件推荐系统_flask+Hadoop+spider
  • 【递归与回溯深度解析:经典题解精讲(下篇)】—— Leetcode
  • Spring boot处理跨域问题
  • 每日小题打卡
  • RockyLinux介绍及初始化
  • 2024年12月青少年软件编程(C语言/C++)等级考试试卷(三级)
  • 【Leecode】Leecode刷题之路第92天之反转链表II
  • StableAnimator模型的部署:复旦微软提出可实现高质量和高保真的ID一致性人类视频生成
  • 3.阿里云flinkselectdb-py作业
  • MATLAB语言的网络编程
  • 深入浅出 Linux 操作系统
  • golang实现生产者消费者模式
  • 自动化测试-Pytest测试
  • Ingress-Nginx Annotations 指南:配置要点全方面解读(下)
  • 【QED】等式构造
  • Kafka数据迁移全解析:同集群和跨集群
  • Debian安装配置RocketMQ
  • vue之axios基本使用
  • 三只脚的电感是什么东西?
  • 【数据库学习笔记】SQL触发器(例题+代码)
  • Unittest02|TestSuite、TestRunner、HTMLTestRunner、处理excel表数据、邮件接收测试结果
  • BAPI_BATCH_CHANGE在更新后不自动更新批次特征