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
What do if you want the doDownload to decide whether or not to send a PDF or Excel file, can one change the content type and filename of the fileDownloadActionListener in the doDownload?
ReplyDeleteCould you present a choice list as part of your page for the user to select PDF or Excel? Then based on the user choice you do the appropriate download in the doDownload(). Same for the filename - the user could enter it as part of your page's UI. That means using bindings for the filename attribute instead of hardcoding it.
ReplyDeletehow to set the file name dynamically from managed bean. its kind of static, but how can we change the file name from backend .
ReplyDelete