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);
List
files = 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());
上一篇:RuoYi-表格按钮传递对象参数
下一篇:RuoYi-Vue前端分页

发表评论

最新留言

很好
[***.229.124.182]2025年04月17日 18时36分02秒