Example:
In .jsf page:
<af:commandbutton id="buttonid" " text="Download" ...
<af:filedownloadactionlistener contenttype="application/octet-stream"
method="#{backingBean.doDownload}" filename="defaultFilename.txt"/>
</af:commandButton>
In backing bean:
public void doDownload(FacesContext facesContext, OutputStream outputStream) {
// write the neccessary code to get the download data from the Model
String data = getDownloadData();
// save to the output stream
try {
OutputStreamWriter writer = new OutputStreamWriter(outputStream,"UTF-8");
writer.write(data);
writer.close();
outputStream.close();
} catch (IOException e) {
// handle I/O exceptions
}
}
Context:
JSF Page
Backing Bean
public void doDownload(FacesContext facesContext, OutputStream outputStream) {
// write the neccessary code to get the download data from the Model
String data = getDownloadData();
// save to the output stream
try {
OutputStreamWriter writer = new OutputStreamWriter(outputStream,"UTF-8");
writer.write(data);
writer.close();
outputStream.close();
} catch (IOException e) {
// handle I/O exceptions
}
}
Context:
JSF Page
Backing Bean