作者 lixiang

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

@@ -450,6 +450,68 @@ export const JeecgListMixin = { @@ -450,6 +450,68 @@ export const JeecgListMixin = {
450 let url = getFileAccessHttpUrl(text) 450 let url = getFileAccessHttpUrl(text)
451 window.open(url); 451 window.open(url);
452 }, 452 },
  453 + //多个文件使用“,” 进行分割,分别进行下载
  454 + downloadFileBatch(text) {
  455 + if (!text) {
  456 + this.$message.warning("未知的文件");
  457 + return;
  458 + }
  459 +
  460 + // 将 text 按逗号分割成多个文件的 URL
  461 + const fileUrls = text.split(",");
  462 +
  463 + // 遍历每个文件的 URL
  464 + fileUrls.forEach((fileUrl, index) => {
  465 + // 去除 URL 两端的空格
  466 + fileUrl = fileUrl.trim();
  467 +
  468 + // 检查 URL 是否有效
  469 + if (!fileUrl) {
  470 + this.$message.warning(`第 ${index + 1} 个文件 URL 无效`);
  471 + return; // 跳过当前文件,继续下一个
  472 + }
  473 +
  474 + // 生成文件访问 URL
  475 + let url = getFileAccessHttpUrl(fileUrl);
  476 +
  477 + // 检查生成的 URL 是否有效
  478 + if (!url) {
  479 + this.$message.warning(`第 ${index + 1} 个文件无法生成访问链接`);
  480 + return; // 跳过当前文件,继续下一个
  481 + }
  482 +
  483 + // 使用 fetch 下载文件
  484 + fetch(url)
  485 + .then(response => {
  486 + if (!response.ok) {
  487 + throw new Error(`第 ${index + 1} 个文件下载失败`);
  488 + }
  489 + return response.blob(); // 将响应转换为 Blob 对象
  490 + })
  491 + .then(blob => {
  492 + // 创建 Blob URL
  493 + const blobUrl = URL.createObjectURL(blob);
  494 +
  495 + // 创建 <a> 标签并触发下载
  496 + const link = document.createElement("a");
  497 + link.href = blobUrl;
  498 + link.download = fileUrl; // 替换为实际文件名
  499 + link.style.display = "none";
  500 + document.body.appendChild(link);
  501 + link.click();
  502 + document.body.removeChild(link);
  503 +
  504 + // 释放 Blob URL
  505 + URL.revokeObjectURL(blobUrl);
  506 + })
  507 + .catch(error => {
  508 + this.$message.error(`第 ${index + 1} 个文件下载失败`);
  509 + console.error(`下载错误:`, error);
  510 + });
  511 + });
  512 + },
  513 +
  514 +
453 }, 515 },
454 516
455 517