动态列表添加附件方法
<input style="width: 80%" type="file" id="file{{idx}}" name="companyOwnershipList[{{idx}}].bankFiles" onchange="fileLimit(this),checkSize(this);"/>
<input type="hidden" name="companyOwnershipList[{{idx}}].attachment" value="{{row.attachment}}"/>
<a title="<spring:message code='document.preview'/>" href="javascript:view('{{row.attachment.id}}')">{{row.attachment.originalName}}</a>
<a id="bankAtt" style="display:none" title="<spring:message code='document.download'/>" href="javascript:attDownloadFile('{{row.attachment.id}}')"><spring:message code="document.download"/></a>
后台处理
保存附件的方法在for循环内
//保存附件
if (companyOwnership.getBankFiles()!=null){
MultipartFile[] bankFiles = companyOwnership.getBankFiles();
if (companyOwnership.getBankFiles().length != 0){
MultipartFile bankFile = bankFiles[0];
if (!bankFile.getOriginalFilename().equals("")){
sysAttachmentService.saveBussAttach(companyOwnership.getBankFiles(), companyOwnership.getId(), "companyOwnership_file_receive");
}
}
}
如果是从其他地方沉淀过来的附件信息可能会出现无法保存到数据库的问题
可以在form方法中把原ID保存到现有的数据某个字段中带到表单内 再从save方法中获取表单原ID数据,
之后查询原ID下的附件,再判断现有数据下有没有附件 如果没有 查询原附件 保存到新附件中 注意附件ID置空
for (CompanyOwnership companyOwnership : companyOwnershipList) {
/*
* 原股东ID
*/
String ownershipId = companyOwnership.getId();
if(StringUtils.isNotBlank(companyOwnership.getCompanyId())){
companyOwnership.setComid(companyOwnership.getCompanyId());
companyOwnership.setType("2");
companyOwnership.setVoting(id);
companyOwnership.setStatus("1");
companyOwnership.setCompanyChangId(companyChange.getId());;
}
companyOwnershipService.save1(companyOwnership);
//保存附件
if (companyOwnership.getBankFiles()!=null){
MultipartFile[] bankFiles = companyOwnership.getBankFiles();
if (companyOwnership.getBankFiles().length != 0){
MultipartFile bankFile = bankFiles[0];
if (!bankFile.getOriginalFilename().equals("")){
sysAttachmentService.saveBussAttach(companyOwnership.getBankFiles(), companyOwnership.getId(), "companyOwnership_file_receive");
}else if (!companyOwnership.getColumn3().equals("") && companyOwnership.getColumn3()!=null){
//如果附件为空,查找交割中的附件
SysAttachment sysAttachmentReceive = new SysAttachment();
sysAttachmentReceive.setBusinessCode("companyOwnership_file_receive");
sysAttachmentReceive.setBusinessId(companyOwnership.getColumn3());
List<SysAttachment> sysAttachments = sysAttachmentService.findList(sysAttachmentReceive);
if (sysAttachments.size()!=0){
SysAttachment sysAttachment2 = sysAttachments.get(0);
sysAttachment2.setId(null);
sysAttachment2.setBusinessId(companyOwnership.getId());
sysAttachmentService.save(sysAttachment2);
}
}
}
}
}
前端附件回显时 隐藏上传附件按钮
if(data[i].attachment != null && data[i].attachment.id != null){
$("#file"+i).hide();
}