Tuesday, May 25, 2010

Downloading a File

Downloading a File:

To a button or a Command link you have to add a File Download Action Listener (af:fileDownloadActionListener). This component can be found under the Operations tab in the component pallete.

The page source code will look like:

<af:commandLink text="#{row.bindings.UserFileName.inputValue}"
shortDesc="#{bindings.SadAttachmentsVO1.hints.UserFileName.tooltip}"
id="it2">
<af:fileDownloadActionListener method="#{ConstituentData_BackingBean.handleFilekDownload}"
filename="#{row.bindings.UserFileName.inputValue}"
contentType="#{row.bindings.FileType.inputValue}"/>
</af:commandLink>


The properties to set are:
- ContentType: the MIME type of the file. This can be retrieved from a VO property is it has been stored along with the file
- Filename: name of the file. Again it should be available along with the file.
- Method: A call to a method on the backing bean, that will prepare the output Stream.

Example: In this method the file data is received as byte array by calling an AM service method. It is then passed onto the output stream.

public void handleFileDownload (FacesContext facesContext, OutputStream outputStream){
try {
Object result = executeMethodWithResult("getCachedAttachment");
outputStream.write((byte[])result);
} catch (IOException e) {
System.out.println("Exception "+e);
}
}

No comments:

Post a Comment