方式一:请求后台代码方式
waf.doPost({
action:"AttachmentListUIService",
data:{
methodName: "getFiles",
ids: ids
},
success:function(result){
for(var j=0;j<result.length;j++){
var dataMap = result[j];
var fileByte = dataMap.fileByte;
var arr = new Uint8Array(fileByte.length);
for (var i = 0;i<fileByte.length;i++){
arr[i] = fileByte[i];
}
var fileName = dataMap.fileName;
var url = window.URL.createObjectURL(new Blob([arr]));
var link = document.createElement('a');
link.style.display = 'none';
link.href = url;
link.setAttribute('download', fileName);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
});
private HashSet getFilesObj(KDActionEvent event) throws EASBizException, BOSException {
HashSet dataSet = new HashSet();
String[] ids = (String[])event.getReqeustContext().getHttpServletRequest().getParameterMap().get("ids[]");
HashSet<String> idSet = new HashSet<String>(Arrays.asList(ids));
EntityViewInfo view = new EntityViewInfo();
FilterInfo filterInfo = new FilterInfo();
filterInfo.getFilterItems().add(new FilterItemInfo("id",idSet,CompareType.INCLUDE));
view.setFilter(filterInfo);
AttachmentCollection coll = AttachmentFactory.getRemoteInstance().getAttachmentCollection(view);
for(int i=0;i<coll.size();i++){
HashMap<String,Object> dataMap = new HashMap<String,Object>();
AttachmentInfo attachmentInfo = coll.get(i);
attachmentInfo = AttachmentFactory.getRemoteInstance().getAttachmentInfo(new ObjectUuidPK(attachmentInfo.getId()));
String fileName = attachmentInfo.getName();
String simpleName = attachmentInfo.getSimpleName();
byte[] fileBtye = null;
AttachmentStorageTypeEnum storageType = attachmentInfo.getStorageType();
if(storageType.equals(AttachmentStorageTypeEnum.FTP)){
FtpConfigInfo ftp = attachmentInfo.getFtp();
ftp = FtpConfigFactory.getRemoteInstance().getFtpConfigInfo(new ObjectUuidPK(ftp.getId()));
String remotePath = attachmentInfo.getRemotePath();
byte[] file = FtpHandleFacadeFactory.getRemoteInstance().download(ftp, remotePath);
}else if(storageType.equals(AttachmentStorageTypeEnum.DATABASE)){
fileBtye = attachmentInfo.getFile();
}
dataMap.put("fileName", fileName+"."+simpleName);
dataMap.put("fileByte", fileBtye);
dataSet.add(dataMap);
}
return dataSet;
}
方式二:标准web链接下载方式
downloadFiles: function (e) {
var ids = waf("#queryGrid").wafGrid("getSelectedRows");
if (ids == null || ids == "") {
_self.showWarning("请选择需要下载的附件。");
return false;
}
for (var i = 0; i < ids.length; i++) {
var fileName = waf("#queryGrid").wafGrid("getCell", ids[i], "name");
var fileType = waf("#queryGrid").wafGrid("getCell", ids[i], "simpleName");
var link = document.createElement('a');
link.style.display = 'none';
link.href = "/easweb/webviews/webframework/webcom/attachment/download.jsp?bosID=" + ids[i];
link.setAttribute('data_url', ids[i]);
link.setAttribute('target', "_blank");
link.setAttribute('download', fileName + "." + fileType);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}