Friday, 24 July 2015

ADF- Download file using fileDownloadActionListener

If you want to download file, you can achieve this using fileDownloadActionListener in ADF application.
Below are the steps to download file.


 1.    Add fileDownloadActionListener to your “Download” button:

                                <af:button text="DownLoad" id="b5"  inlineStyle="float: right;">
                                                <af:fileDownloadActionListener filename="abc.txt"   contentType="text/plain; charset=utf-8"
                                                           method="#{<your method>}"/> </af:button>

2.     In your managed bean method add below code:
        
         public void yourmethod(FacesContext facesContext, OutputStream outputStream){

            File file = new File("E:\\templates\\abc.txt");
            FileInputStream fdwd;
            byte[] bt;
            try {
                fdwd = new FileInputStream(file);
                int checkline;
                while ((checkline = fdwd.available()) > 0) {
                    bt = new byte[checkline];
                    int rst = fdwd.read(bt);
                    outputStream.write(bt, 0, bt.length);
                    if (rst == -1)
                        break;
                }
                outputStream.flush();
            } catch (IOException e) {
                ….
            }
      }
Note: This will download file from your file system.

3.  If you want to download file from database, write below code:

                     public void downloadChart(FacesContext facesContext, OutputStream outputStream)                             {
                          // get row containing file that you want to download
                         blob = (BlobDomain) row.getAttribute("DataFile");
                           try {
                             IOUtils.copy(blob.getInputStream(), outputStream);
                            blob.closeInputStream();
                                outputStream.flush();
                          } catch (IOException e) {
                               e.printStackTrace();
   FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,                  e.getMessage(), "");
                            facesContext.addMessage(null, msg);
                          }
                       }


Note: you have to get the row containing file.


Hope this will be helpful
Ravi


1 comment:

  1. Hi, can you post a sample of how to actually get the row containing the file ? For example if i have a af Table built in mbean based on List elements and the CustomClass emenets has one field of type blob - how can i select and download one of the blobs from one specific row in that table? Thank you

    ReplyDelete