File attachment using Adf faces, EJB3 and Oracle XE database Part IV
continued...
Downloading file
Following is the code in AttachmentBackingBean
Deleting File
You can download the source from FileUploadDemoBlog.zip
I would like to say special thanks to my team members specially Pino and Owie for their support.
I will appreciate anyone who comments and let me know the more appropriate way for doing the same task.
Downloading file
In download button, add FileDownloadActionListener and select fileDownloadActionListener from backingBean |
The binding of attachment page should looks like above. |
Following is the code in AttachmentBackingBean
public void downloadAction(FacesContext facesContext, OutputStream outputStream) { Attachment att = (Attachment)attachmentList.getSelectedRowData(); ExternalContext ectx = facesContext.getExternalContext(); String fileName = null; byte[] contents = null; if (att == null) { return; } else if (att.getAttachmentId() == null) { fileName = att.getFileName(); contents = att.getFileContent(); } else { OperationBinding oper = getBindings().getOperationBinding("findAttachmentById"); oper.getParamsMap().put("attachmentId", att.getAttachmentId()); Attachment attachment = (Attachment)oper.execute(); fileName = attachment.getFileName(); contents = attachment.getFileContent(); } BlobDomain content = new BlobDomain(); content.setBytes(contents); Long length = content.getLength(); HttpServletResponse response = (HttpServletResponse)ectx.getResponse(); response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\""); response.setContentLength(length.intValue()); try { InputStream in = content.getBinaryStream(); byte[] buf = new byte[1024]; int count; while ((count = in.read(buf)) >= 0) { outputStream.write(buf, 0, count); } in.close(); outputStream.flush(); outputStream.close(); facesContext.responseComplete(); } catch (IOException ex) { System.out.println(ex.getMessage()); ex.printStackTrace(); } } |
Your binding of attachment page should be like this. |
On delete File button, provide the actionListener and bellow is the code for actionListener |
public void deleteFile(ActionEvent actionEvent) { Attachment attachment = (Attachment)attachmentList.getSelectedRowData(); if ( attachment != null ) { Listattachments = (List )AdfFacesContext.getCurrentInstance().getPageFlowScope().get("attachmentList"); attachments.remove(attachment); AdfFacesContext.getCurrentInstance().addPartialTarget(attachmentList); } }
You can download the source from FileUploadDemoBlog.zip
I would like to say special thanks to my team members specially Pino and Owie for their support.
I will appreciate anyone who comments and let me know the more appropriate way for doing the same task.
Hi Adnan,
ReplyDeleteThanks for your post. I was exactly looking for something like this.
The link for source code isn't working. Can you please share the code?
Thanks,
Gaurish
Hello Gaurish,
ReplyDeleteI will upload the source code on some other server and will update the link. thank you for your comment
HI Adnan,
ReplyDeleteThis post looks good. I want to test this scenario.
The link for source code isn't working. Can you please share the code?
Thanks,
Kishore.
Hi Kishore, I have updated the link
ReplyDeletethank you
Adnan
Hi Adnan.. i am new to ADF and have been searching for something like this. i checked the example and it is really very easy to understand. I even tried it out. only the difference that i cant use entities. instead using DAO. and couldnt find a way of getting through.. Could you please suggest me what i can do ?
ReplyDeleteHi Adnan,
ReplyDeleteThis example is very good but i can't do it on below versions
1-Oracle JDeveloper 11g Release2 (11.1.2.3)
2-Oracle SQL Developer (3.2.10.09)
3-Oracle Enterprise Manager 11g database control
Please i need your advice for File attachmemt on the above versions.
Thank you.