File attachment using Adf faces, EJB3 and Oracle XE database Part IV

continued...
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.
Deleting File

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 ) {
       List  attachments = (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.

Comments

  1. Hi Adnan,

    Thanks 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

    ReplyDelete
  2. Hello Gaurish,
    I will upload the source code on some other server and will update the link. thank you for your comment

    ReplyDelete
  3. HI Adnan,
    This 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.

    ReplyDelete
  4. Hi Kishore, I have updated the link
    thank you
    Adnan

    ReplyDelete
  5. 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 ?

    ReplyDelete
  6. Hi Adnan,

    This 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.

    ReplyDelete

Post a Comment

Popular posts from this blog

Unable to create a server socket for listening on channel "Default[4]". The address 0:0:0:0:0:0:0:1 might be incorrect or another process is using port 8001: java.net.BindException: Address already in use: JVM_Bind.

[Deployer:149164]The domain edit lock is owned by another session in exclusive mode - hence this deployment operation cannot proceed.

Database Connection issues in Jdeveloper with MySQL Database