Search code examples
jsfdownloadjsf-1.1

How to download a file from webapp folder using JSF 1.1?


I am using JSF 1.1. On click of a button I need to download a file from webapp/pdf directory. How can I achieve this?


Solution

  • Just link to its URL directly. Both the server and the browser will do the necessary magic.

    The desired HTML output should look like this:

    <a href="/yourcontext/pdf/filename.pdf">
        Download PDF
    <a>
    

    The JSF 1.1 on JSP way to generate this HTML is:

    <h:outputLink value="${pageContext.request.contextPath}/pdf/filename.pdf">
        <h:outputText value="Download PDF" />
    </h:outputLink>
    

    Or, when you're using JSF 1.1 on Facelets:

    <h:outputLink value="#{request.contextPath}/pdf/filename.pdf">
        Download PDF
    </h:outputLink>
    

    If necessary, throw in some CSS to make it look like a "button".