
RuoYi-Vue以流的方式下载文件
发布日期:2021-05-14 19:29:27
浏览次数:23
分类:精选文章
本文共 3097 字,大约阅读时间需要 10 分钟。
������������������������������������������
1. ���������������������
������������������������������������������������������
import service from "@/utils/request";
������������������������������������������������������������service
������������������������������������������������������
2. ������������������������
������������������������������������������������������������������������������������������������������
methods: { /** * ������������������������ * @param name ��������� */ handleDownload(name) { const filename = name; return service .post("/monitor/logger/file/download/" + filename, {}, { headers: { "Content-Type": "application/x-www-form-urlencoded", }, responseType: "blob", }) .then((data) => { const fileReader = new FileReader(); fileReader.onload = () => { try { const { code, msg } = JSON.parse(fileReader.result); if (code !== 200) { this.msgError(msg || "������������"); } } catch (err) { //������������������������������������ const content = data; const blob = new Blob([content]); if ("download" in document.createElement("a")) { const elink = document.createElement("a"); elink.download = filename; elink.style.display = "none"; elink.href = URL.createObjectURL(blob); document.body.appendChild(elink); elink.click(); URL.revokeObjectURL(elink.href); document.body.removeChild(elink); } else { navigator.msSaveBlob(blob, filename); } } }; fileReader.readAsText(data); }) .catch((r) => { console.error(r); }); },}
3. ������������������
���������������������������/file/download/{fileName}
������������
@PostMapping("/file-download/{fileName}")public void download(@PathVariable("fileName")String fileName, HttpServletResponse response) throws Exception { // ������������ if (StringUtils.isEmpty(fileName)) { throw new Exception("������������������"); } // ������������������������ String logPath = getLogPath(); if (StringUtils.isEmpty(logPath)) { throw new Exception("���������������������������"); } File directory = new File(logPath); Listfiles = FileUtils.listFiles(directory, null, true); String filePath = ""; for (File file : files) { if (file.getName().equals(fileName) && file.isFile() && file.exists()) { filePath = file.getPath(); break; } } if (StringUtils.isEmpty(filePath)) { throw new Exception("���������������������"); } response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); FileUtils.writeBytes(filePath, response.getOutputStream());}
������������������������
��������� JourneyController���������������������������
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);FileUtils.writeBytes(filePath, response.getOutputStream());
发表评论
最新留言
很好
[***.229.124.182]2025年04月17日 18时36分02秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
tinyproxy正向代理
2021-05-16
codeforces 59A-C语言解题报告
2021-05-16
Fast Matrix Calculation HDU-4965 矩阵快速幂
2021-05-16
OpenCv 金字塔之上采样与下采样
2021-05-16
华为手机麦芒9参数配置
2021-05-16
Java中的注释
2021-05-16
Linux+Nginx+Asp.net Core及守护进程部署
2021-05-16
计算机网络--大厂常见问题(2)
2021-05-16
2021-05-12书城项目第一阶段:提交表单
2021-05-16
cookie、session、token
2021-05-16
vue项目更改title
2021-05-16
IDEA-@Slf4j和log标签&@Data(Lombok)无效
2021-05-16
Thymeleaf 生成下标,索引,使用Stat变量
2021-05-16
全局变量初始化顺序的不确定性引发的bug
2021-05-16
【C++11】std::function和std::bind
2021-05-16
java基础避坑(二)——基础数据类型short
2021-05-16
Hyper ledger Caliper官方文档【中文版】
2021-05-16
以太坊NFT通证制作工具【EIP-721/1155】
2021-05-16