It is common requirement in many projects to have file download facility.
File download facility can be achieved by many means, but in 11g + versions we have one very good component
to handle file download facility very quickly.
In 11g + versions we have one listener component called fileDownloadActionListener. Which can be triggered from any command related components like commandButton , commandLink etc.
It has three parameters filename, contentType, method.
filename : It is used to display the file name in response
method: The method will be present in managed bean. It is used to handle the file download related activities. Signature of this method is
public void <method name> (FacesContext context, OutputStream out) throws IOException
{
...... your code...
}
contentType: It is used to specify the content type.(like it is word document or pdf document etc.)
In the below example I have used fileDownloadActionListener with commandLink.
<af:commandLink id="cl1" text="#{row.fileName}"
partialSubmit="true"
binding="#{fileDownloadBean.downloadLink}">
<af:fileDownloadActionListener filename="#{row.fileName}"
method="#{fileDownloadBean.download}" ></af:fileDownloadActionListener>
</af:commandLink>
The method referred is
public void download(FacesContext context,
OutputStream out) throws IOException {
File f =
new File(this.directoryPath.getValue().toString() + "/" + downloadLink.getText());
FileInputStream fis;
byte[] b;
try {
fis = new FileInputStream(f);
int n;
while ((n = fis.available()) > 0) {
b = new byte[n];
int result = fis.read(b);
out.write(b, 0, b.length);
if (result == -1)
break;
}
} catch (IOException e) {
e.printStackTrace();
}
out.flush();
}
So whatever file I want to download it will be downloaded using this listener. The coding type will be least one.
Please comment...
File download facility can be achieved by many means, but in 11g + versions we have one very good component
to handle file download facility very quickly.
In 11g + versions we have one listener component called fileDownloadActionListener. Which can be triggered from any command related components like commandButton , commandLink etc.
It has three parameters filename, contentType, method.
filename : It is used to display the file name in response
method: The method will be present in managed bean. It is used to handle the file download related activities. Signature of this method is
public void <method name> (FacesContext context, OutputStream out) throws IOException
{
...... your code...
}
contentType: It is used to specify the content type.(like it is word document or pdf document etc.)
In the below example I have used fileDownloadActionListener with commandLink.
<af:commandLink id="cl1" text="#{row.fileName}"
partialSubmit="true"
binding="#{fileDownloadBean.downloadLink}">
<af:fileDownloadActionListener filename="#{row.fileName}"
method="#{fileDownloadBean.download}" ></af:fileDownloadActionListener>
</af:commandLink>
The method referred is
public void download(FacesContext context,
OutputStream out) throws IOException {
File f =
new File(this.directoryPath.getValue().toString() + "/" + downloadLink.getText());
FileInputStream fis;
byte[] b;
try {
fis = new FileInputStream(f);
int n;
while ((n = fis.available()) > 0) {
b = new byte[n];
int result = fis.read(b);
out.write(b, 0, b.length);
if (result == -1)
break;
}
} catch (IOException e) {
e.printStackTrace();
}
out.flush();
}
So whatever file I want to download it will be downloaded using this listener. The coding type will be least one.
Please comment...
Simple Solution ... but very effective ... I got the blob using a method call on Application Module and this helped me to reach the UI ..
ReplyDeleteThanks a ton
Happy Learning .
Isaiah
SSE | Ethical Hacker
I am very glad to know that the post helped you a lot, thanks .....
ReplyDeleteBut the component has a problem. It generate the following error when the page it's refreshed. DO you hnow another alternative?
ReplyDeleteADF_FACES-60003: Component with ID: %s not registered for Active Data
Can you please be elaborate on this. I have not faced such a problem yet ...
ReplyDeleteDo we have a similar component for viewing the file content? The requirement we have is to show the file content of the file that has been fetched from the db and the file can have any extention(xls,doc,rar,zip etc).
ReplyDeleteI have a question - what java type is fileDownloadBean.downloadLink ?
ReplyDeleteThis example is not working...... :(
ReplyDeleteplease give complete source code...
Hi Moinak ,
ReplyDeletePlease give us download link for this example
I want to ask, how to refresh the page content after downloading a file with af:fileDownloadActionListener?
ReplyDeleteMy scenario is the following:
I use af:commandButton with attached af:fileDownloadActionListener in order to export into file some data from ADF RowIterator. During the export I count the number of the rows successfully added into file and then I want to display this number in the page. I set the value for row count to af:outputText component, but the page is not refreshed until I press browser's F5 button.
In the ADF documentation I have read this:
"file download must be processed with an ordinary request instead of the XMLHttp AJAX requests, the parent component’s partialSubmit attribute, if supported, must be set to false"
I set the parent component (af:commandButton) with partialSubmit="false" and I think that this means: no partial page submit and the full page will be refreshed. But why I couldn't see the new values until I press F5?
In your example above af:commandLink is with partialSubmit="true". Why?
Thanks at advance, if you can give me some explanation about page refresh after using .
I too am having a problem with the page refresh, partial triggers or any kind of action after the download... is there any way to get any kind of event to notification after the download so a partial trigger, (or partial target in code) will work?
ReplyDeleteIt is not work well with layer maps.
ReplyDeletePlease provide the complete code.
ReplyDeleteit is not work well with not Adf falie
ReplyDeletesachin tiwari
please complete the source code ,it doesnt work correctly.
ReplyDeletethanks.
how can i download this projecet?
ReplyDeleteHow to avoid full page refresh in download listener . if is giving error unregistered components due to full page refresh
ReplyDeleteHow to avoid full page refresh in download listener . if is giving error unregistered components due to full page refresh
ReplyDeleteHow to avoid full page refresh in download listener . if is giving error unregistered components due to full page refresh
ReplyDeleteHow to avoid full page refresh in download listener . if is giving error unregistered components due to full page refresh
ReplyDeleteThanks for sharing this great information I am impressed by the information that you have on this blog. Same as your blog i found another one Oracle ADF .
ReplyDeleteActually, I was looking for the same information on internet for
Oracle ADF Interview Questions and Answers/Tips and came across your blog. I am impressed by the information that you have on this blog. It shows how well you understand this subject.
Thanks for sharing this blog. The content is beneficial and useful. Very informative post. Visit here to learn more about Data Warehousing companies and Data analytics Companies. I am impressed by the information that you have on this blog. Thanks once more for all the details.Visit here for Top Big Data Companies.
ReplyDelete