Monday, October 18, 2010

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.

4 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