Monday, 11 April 2011

File Download In ADF

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...

21 comments:

  1. Simple Solution ... but very effective ... I got the blob using a method call on Application Module and this helped me to reach the UI ..

    Thanks a ton
    Happy Learning .
    Isaiah
    SSE | Ethical Hacker

    ReplyDelete
  2. I am very glad to know that the post helped you a lot, thanks .....

    ReplyDelete
  3. But the component has a problem. It generate the following error when the page it's refreshed. DO you hnow another alternative?

    ADF_FACES-60003: Component with ID: %s not registered for Active Data

    ReplyDelete
  4. Can you please be elaborate on this. I have not faced such a problem yet ...

    ReplyDelete
  5. Do 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).

    ReplyDelete
  6. I have a question - what java type is fileDownloadBean.downloadLink ?

    ReplyDelete
  7. This example is not working...... :(
    please give complete source code...

    ReplyDelete
  8. Hi Moinak ,
    Please give us download link for this example

    ReplyDelete
  9. I want to ask, how to refresh the page content after downloading a file with af:fileDownloadActionListener?

    My 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 .

    ReplyDelete
  10. 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?

    ReplyDelete
  11. It is not work well with layer maps.

    ReplyDelete
  12. Please provide the complete code.

    ReplyDelete
  13. it is not work well with not Adf falie
    sachin tiwari

    ReplyDelete
  14. please complete the source code ,it doesnt work correctly.
    thanks.

    ReplyDelete
  15. how can i download this projecet?

    ReplyDelete
  16. How to avoid full page refresh in download listener . if is giving error unregistered components due to full page refresh

    ReplyDelete
  17. How to avoid full page refresh in download listener . if is giving error unregistered components due to full page refresh

    ReplyDelete
  18. How to avoid full page refresh in download listener . if is giving error unregistered components due to full page refresh

    ReplyDelete
  19. How to avoid full page refresh in download listener . if is giving error unregistered components due to full page refresh

    ReplyDelete
  20. Thanks 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 .
    Actually, 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.

    ReplyDelete
  21. 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