miércoles, 6 de febrero de 2013

How download a file whit adf components


Some times when we need download a file stored in any place, for example a web service. In this case file was generated from a web service andstored in a byte array.

To download the file first need a command button or a command link. I used the comand link.

 <af:commandLink text="some text"
        id="cl1" rendered="true">
        <af:fileDownloadActionListener method="#{someMB.fileDownload}" 

         filename="#{someMB.fileName}"/>
</af:commandlink>

And in the managed bean

    public void fileDownload(FacesContext facesContext,
                                        OutputStream outputStream) {
 

        // Asume we already have a byte array
            try {
                outputStream.write(data, 0, data.length);
                outputStream.flush();
            } catch (IOException e) {
                generateErrorMessage("Error downloading file);
            }
       
    }

   
 


That is, the fileDownloadActionListener provides the mechanism to download the file.

NOTE: A limitation of the fileDownloadActionListener tag exists in Internet Explorer if the filename is multibyte, like Japanese characters. If the filename is multibyte and does not contain an ascii extension (e.g., .txt or .doc), then the filename will not display correctly in the file download box. More
 

No hay comentarios.:

Publicar un comentario