Oracle Fusion Middleware Tag Reference for Oracle ADF Faces 12c (12.2.1.4.0) E81455-03

# <af:fileDownloadActionListener>

fileDownloadActionListener filedownloadactionlistenerfiledownloadaction

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.

This example sends a simple "Hello there!" file to the user.

##

                <af:commandButton text="Say Hello">
                  <af:fileDownloadActionListener filename="hello.txt"
                                            contentType="text/plain; charset=utf-8"
                                            method="#{bean.sayHello}"/>
                </af:commandButton>

        public void sayHello(FacesContext context, OutputStream out) throws IOException
        {
          OutputStreamWriter w = new OutputStreamWriter(out, "UTF-8");
          w.write("Hi there!");
          // The stream is automatically closed, but since we wrapped it,
          // we'd better flush our writer
          w.flush();
        }

     Once the actionListener is processed responseComplete() is called, indicating to the JavaServer Faces
        implementation that the HTTP response for this request has already been generated
        (such as an HTTP redirect), and that the request processing lifecycle should be terminated
        as soon as the current phase is completed (which typically is the InvokeApplication phase).

         // pseudo-code below
        String filename = getFilename();
        String contentType = getContentType();

        HttpServletResponse hsr = (HttpServletResponse) response;
        try
        {
          hsr.setContentType(contentType);
          if (filename != null)
          {
            ...
            hsr.setHeader("Content-Disposition",
                          "attachment; filename=\""+encodeHTTPHeaderFilename + "\"");

          }
          MethodExpression method = getMethod();
          OutputStream out = new BufferedOutputStream(hsr.getOutputStream());
          // the method you bound to af:fileDownloadActionListener is called here
          method.invoke(context.getELContext(), new Object[]{context, out});
          out.close();
        }
        catch (Exception e)
        {
          hsr.reset();
          context.addMessage(null, errorMessage);
          context.renderResponse();
          return;
        }
      }
      context.responseComplete();

     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.

    Attributes

        Name
        Type
        Supports EL?
        Description

    contentType
    String
    Yes

        the MIME type of the file, for example text/plain, text/csv,
        application/pdf, etc.

    filename
    String
    Yes

         the proposed filename for the object.  When set, a "Save File"
         dialog will typically be displayed, though this is ultimately up to
         the browser.  If not set, the content will typically be displayed
         inline in the browser if possible.

    method
    MethodExpression
    Only EL

         the method that will be used to download the file contents.
         The method takes two arguments, a FacesContext and an OutputStream.
         The OutputStream will be automatically closed, so the sole responsibility
         of this method is to write all bytes to the OutputStream.