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

vue+srpingboot实现多文件导出

项目场景:

vue+srpingboot实现多文件导出


解决方案:

直接上干货

<el-button type="warning" icon="el-icon-download" size="mini" class="no-margin"  @click="exportSelectedFiles" :disabled="selectedRows.length === 0">导出</el-button>
    async exportSelectedFiles() {if (this.selectedRows.length === 0) {this.$message.warning('请至少选择一条记录');return;}// 提取所有选中的文件的 idconst fileIds = this.selectedRows.map(row => row.id);// 请求文件路径数据const params = { id: fileIds };const fileResponse = await url.getFilePatchData(params);if (fileResponse.data && fileResponse.data.length > 0) {// 提取文件路径const filePaths = fileResponse.data.map(file => file.filepath);this.downloadMultipleFiles(filePaths);} else {this.$message.error('无法获取文件路径');}},

const fileResponse = await url.getFilePatchData(params);这行代码其实就是一个查询接口

选择这条数据将他下面所属的文档路径查询出来 然后调用下载文档的方法
 

// 下载多个文件(打包为ZIP)downloadMultipleFiles(filePaths) {const params = { filePaths: filePaths };url.downloadMultiple(params).then(response => {// 关键修复:确保正确解析二进制流const blob = new Blob([response], { type: 'application/zip' });if (blob.size === 0) {this.$message.error('服务器返回空文件');return;}const downloadUrl = window.URL.createObjectURL(blob);const link = document.createElement('a');link.href = downloadUrl;link.download = `documents_${Date.now()}.zip`;document.body.appendChild(link);link.click();// 延迟清理以避免下载中断setTimeout(() => {document.body.removeChild(link);window.URL.revokeObjectURL(downloadUrl);}, 200);}).catch(error => {this.$message.error('文件下载失败: ' + error.message);});},
url.downloadMultiple(params)也是调用的后端接口跟this.axios=url一样
//多条导出zip@PostMapping("/download-multiple")public void downloadMultipleFiles(@RequestBody Map<String, List<String>> requestBody,HttpServletResponse response) throws IOException {response.setContentType("application/zip");response.setHeader("Content-Disposition", "attachment; filename=documents.zip");response.resetBuffer();try (ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()))) {byte[] buffer = new byte[8192];for (String filePath : requestBody.get("filePaths")) {Path file = Paths.get(filePath).normalize();if (!Files.exists(file) || Files.isDirectory(file)) continue;try (InputStream in = new BufferedInputStream(Files.newInputStream(file))) {ZipEntry entry = new ZipEntry(file.getFileName().toString());zipOut.putNextEntry(entry);int bytesRead;while ((bytesRead = in.read(buffer)) != -1) {zipOut.write(buffer, 0, bytesRead);}zipOut.closeEntry();zipOut.flush();}}}}

调用zip的插件导入对应的依赖即可实现多文件导出,导出会保存生成zip压缩包

相关文章:

  • Qt enabled + geometry 属性(2)
  • matlab加权核范数最小化图像去噪
  • 软件设计师“排序算法”真题考点分析——求三连
  • word通配符表
  • 远程连接的公私钥加密
  • Sign签证绕过
  • MobaXterm 解除只能保存14个session会话限制
  • 远程合并其他分支,再提交
  • 软件工程(六):一致性哈希算法
  • labview——控制继电器模块
  • UML基本概念:构造块、公共机制与规则
  • 解决C#泛型类参数无法带参数实例化的问题
  • UML 图的细分类别及其应用
  • python训练营打卡第30天
  • Qwen 3技术报告详细解读
  • junit单元测试
  • RT_Thread——内存管理
  • 融云 uni-app IMKit 上线,1 天集成,多端畅行
  • 什么是国密、密评、商密
  • 离线服务器算法部署环境配置
  • 温州网站建设 首选国鼎网络好/班级优化大师简介
  • nike网站建设分析/营销网站搭建
  • 设计好 英文网站/网站流量
  • 上海热线网站建设/爱站seo工具包下载
  • 衡水建站公司/卡点视频免费制作软件
  • 万网的网站代码怎么看/seo诊断优化方案