作者 lixiang

1、公共方法增加downloadFileBatch函数,可用于单、多次下载文件

... ... @@ -450,6 +450,68 @@ export const JeecgListMixin = {
let url = getFileAccessHttpUrl(text)
window.open(url);
},
//多个文件使用“,” 进行分割,分别进行下载
downloadFileBatch(text) {
if (!text) {
this.$message.warning("未知的文件");
return;
}
// 将 text 按逗号分割成多个文件的 URL
const fileUrls = text.split(",");
// 遍历每个文件的 URL
fileUrls.forEach((fileUrl, index) => {
// 去除 URL 两端的空格
fileUrl = fileUrl.trim();
// 检查 URL 是否有效
if (!fileUrl) {
this.$message.warning(`第 ${index + 1} 个文件 URL 无效`);
return; // 跳过当前文件,继续下一个
}
// 生成文件访问 URL
let url = getFileAccessHttpUrl(fileUrl);
// 检查生成的 URL 是否有效
if (!url) {
this.$message.warning(`第 ${index + 1} 个文件无法生成访问链接`);
return; // 跳过当前文件,继续下一个
}
// 使用 fetch 下载文件
fetch(url)
.then(response => {
if (!response.ok) {
throw new Error(`第 ${index + 1} 个文件下载失败`);
}
return response.blob(); // 将响应转换为 Blob 对象
})
.then(blob => {
// 创建 Blob URL
const blobUrl = URL.createObjectURL(blob);
// 创建 <a> 标签并触发下载
const link = document.createElement("a");
link.href = blobUrl;
link.download = fileUrl; // 替换为实际文件名
link.style.display = "none";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
// 释放 Blob URL
URL.revokeObjectURL(blobUrl);
})
.catch(error => {
this.$message.error(`第 ${index + 1} 个文件下载失败`);
console.error(`下载错误:`, error);
});
});
},
},
... ...