时间:2023-01-15 11:44:00 | 栏目:JAVA代码 | 点击:次
@RequestMapping("downLoad") public ResponseEntity<byte[]> downloadPromisePdf() { String fileName = "企业诚信守法承诺书.pdf"; try { byte[] pdf = **;//byte文件 String dfileName = new String(fileName.getBytes("gb2312"), "iso8859-1"); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentDispositionFormData("attachment", dfileName); return new ResponseEntity<>(pdf, headers, HttpStatus.OK); } catch (BuzEx e){ logger.error( e.getMessage()); }catch (Exception e) { } return null; }
前端直接window.location.href='/downLoad';
后台ResponseEntity代码还是一次请求下载一个
前台js改为发出多个请求
js中先用数组储存需要下载的文件参数信息,然后循环数组执行下载方法,下载方法则先ajax判断文件是否存在,是则动态创建a标签批量下载文件
//获得文件数组ids后 循环下载方法 $.each(ids,function(i,value){ downLoad(fileFunctionPathArray[i],fileNameInServerArray[i],fileOriginalNameArray[i],ids[i]); }) //下载方法 function downLoad(fileFunctionPath,fileNameInServer,fileOriginalName,ids){ $.ajax({ //检查文件是否存在 url: "/ResourceManage/resourceDownloaduserLink/checkPermission", data: { sysuserid: localStorage.getItem("id"), resourceid: ids }, success: function (data) {//文件存在则创建动态a标签批量下载文件 if (data.success) { //ResponseEntity下载文件的url var url = "../filehandle/downLoad.do?filePlatPath=" + "resource&fileFunctionPath=" + fileFunctionPath + "&fileNameInServer=" + fileNameInServer + "&fileOriginalName=" + fileOriginalName; var fileName = fileNameInServer; downloadFile(url,fileName);//动态创建a标签 批量下载 } } }) } //动态创建a标签 const downloadFile = (url, fileName = '') => { let eleLink = document.createElement('a'); eleLink.download = fileName; eleLink.style.display = 'none'; eleLink.href = url; // 受浏览器安全策略的因素,动态创建的元素必须添加到浏览器后才能实施点击 document.body.appendChild(eleLink); // 触发点击 eleLink.click(); // 然后移除 document.body.removeChild(eleLink); };
点击下载则会 批量同时下载