Hello All
This post is about using af:fileDownloadActionListener to generate file on run-time, suppose i have a viewObject and i want to export it's data in CSV or plain text so for this requirement we can generate file at run-time and send it to client UI to download
What is af:fileDownloadActionListener-
as per oracle docs-
The fileDownloadActionListener tag is a declarative way to allow an action source (<commandButton>, <commandLink>, etc.) to programatically send the contents of a file to the user, optionally with a specific content type and filename. Since file downloads must be processed with an ordinary request - not XMLHttp AJAX requests - this tag forces partialSubmit to be false on the parent component, if it supports that attribute.
The fileDownloadActionListener uses the native (browser built-in) filedownload popup, so this popup cannot be configured.
In this post i am using Departments table of HR schema
Cheers:) Happy Learning
This post is about using af:fileDownloadActionListener to generate file on run-time, suppose i have a viewObject and i want to export it's data in CSV or plain text so for this requirement we can generate file at run-time and send it to client UI to download
What is af:fileDownloadActionListener-
as per oracle docs-
The fileDownloadActionListener tag is a declarative way to allow an action source (<commandButton>, <commandLink>, etc.) to programatically send the contents of a file to the user, optionally with a specific content type and filename. Since file downloads must be processed with an ordinary request - not XMLHttp AJAX requests - this tag forces partialSubmit to be false on the parent component, if it supports that attribute.
The fileDownloadActionListener uses the native (browser built-in) filedownload popup, so this popup cannot be configured.
In this post i am using Departments table of HR schema
- Prepare model part using Departments table and drop it on page as a table and add a button to UI for downloading
- drop af:fileDownloadActionListener as child of button and set it's property as contentType, method , fileName
- Now see code written in download Listener-
- Run this application and click on download button, browser download box appears , open downloaded file and see how your data appears in text file, to export data in a pdf file you have to generate PDF file using some API then you can download it in same way
/**Method to get BindingsContainer for current page * @return */ public BindingContainer getBindingsCont() { return BindingContext.getCurrent().getCurrentBindingsEntry(); } /**Method to download ViewObject's data in plain text file * @param facesContext * @param outputStream * @throws UnsupportedEncodingException * @throws IOException */ public void fileDownloadListener(FacesContext facesContext, OutputStream outputStream) throws UnsupportedEncodingException, IOException { OutputStreamWriter w = new OutputStreamWriter(outputStream, "UTF-8"); //Get itertaor from bindings DCIteratorBinding deptIter = (DCIteratorBinding) getBindingsCont().get("Departments1Iterator"); //Get ViewObject from Iterator ViewObjectImpl vo = (ViewObjectImpl) deptIter.getViewObject(); ViewAttributeDefImpl[] attrDefs = vo.getViewAttributeDefImpls(); int count = 0; RowSetIterator rsi = vo.createRowSetIterator(null); while (rsi.hasNext()) { Row nextRow = rsi.next(); if (nextRow != null) { // Code to iterate over ViewObject's column to get all columns value at runtime for (ViewAttributeDefImpl attrDef : attrDefs) { byte attrKind = attrDefs[count].getAttributeKind(); //checks attribute kind for each element in an array of AttributeDefs if (attrKind != AttributeDef.ATTR_ASSOCIATED_ROW && attrKind != AttributeDef.ATTR_ASSOCIATED_ROWITERATOR) { String columnName = attrDef.getName(); w.write(columnName + " - " + nextRow.getAttribute(columnName) + " "); } } // Code to create new line text line for new Row w.write(System.getProperty("line.separator")); } } //Flush the writer after wrting file w.flush(); } }
Cheers:) Happy Learning
Using adf download listerner , full page is getting refresed which caused partial trigger unregistered components error.
ReplyDeletePlease help how to avoid full page refresh In adf download listener .
Set partial submit true for the button and check again
Deletei have tried the partial submit true also. it is also not working. its refreshing the page always.
ReplyDeleteHow to handle the exception and error, when no file is present . its always downloading the blank file when no file / no bytes is present in managed bean.
how to show the error warning in this downloadActionListener.
Capil I have checked and it is not happening in my case , Try using af:link instead of button
DeleteAnd you need to handle that in bean if there is no file present then make that button disable
Ashish