title: Primefaces download file from datatable
body: <p>I have this weird issue with File Download option not being called from the XHTML. I am very new to JSF, so it might be something basic that I am screwing up, but any help would be appreciated.</p>
  
  <p>This is my XHTML</p>
  
  <pre><code>&lt;p&gt;Files List&lt;/p&gt;
  &lt;h:form prependId="false"&gt;
      &lt;p:dataTable value="#{pc_ArchiveFiles.archiveFiles}" var="fs"
          paginator="true" rows="5"
          paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
          rowsPerPageTemplate="5,10,15"&gt;
          &lt;f:facet name="header"&gt;  
              Client Files  
          &lt;/f:facet&gt;
          &lt;p:column&gt;
              &lt;f:facet name="header"&gt;
                  File Name
              &lt;/f:facet&gt;
               &lt;h:outputText value="#{fs.fileName}" /&gt; 
          &lt;/p:column&gt;
          &lt;p:column&gt;
              &lt;f:facet name="header"&gt;
                  File Size
              &lt;/f:facet&gt;
              &lt;h:outputText value="#{fs.fileSizeInKB}" /&gt;
          &lt;/p:column&gt;
          &lt;p:column&gt;
              &lt;f:facet name="header"&gt;
                 Download File
              &lt;/f:facet&gt;
              &lt;p:commandLink id="downloadLink" value="Download" ajax="false"&gt;  
                      &lt;p:fileDownload value="#{pc_ArchiveFiles.downloadPDF}" /&gt;
              &lt;/p:commandLink&gt; 
          &lt;/p:column&gt;
      &lt;/p:dataTable&gt;
  &lt;/h:form&gt;
  </code></pre>
  
  <p>This is the backing bean </p>
  
  <pre><code>@ManagedBean(value = "pc_ArchiveFiles")
  @RequestScoped
  public class ArchiveFiles extends PageCodeBase {
  private static final Logger logger = LoggerFactory
          .getLogger(ArchiveFiles.class);
   @Value("${archive.location}")
  private String repository;
  public List&lt;ArchiveFile&gt; getArchiveFiles() {
  
  
      Map&lt;String, String&gt; params = FacesContext.getCurrentInstance()
              .getExternalContext().getRequestParameterMap();
      subCategory = params.get("categoryName");
  
      // Build request object
      ArchiveFilesRequest request = new ArchiveFilesRequest();
      request.setCaseWorkerId(SecurityUtil.getLoggedInUser().getLoggedUserId());
      request.setClientId(getSelectedConsumer().getConsumerId());
      request.setCategoryName(subCategory);
      ArchiveFilesResponse response = archiveService.getArchiveFiles(request);
      if((response.getResponseType() == ResponseType.SUCCESS || response.getResponseType() == ResponseType.WARNING) &amp;&amp; response.getFileCount() &gt; 0) { // There is at least one file
          archiveFiles = new ArrayList&lt;ArchiveFile&gt;();
          archiveFiles.addAll(response.getFileSet());
          return archiveFiles;
      } else {
          return Collections.&lt;ArchiveFile&gt; emptyList();
      }
  }
  
  
   public void downloadPDF() throws IOException {
  
          FacesContext facesContext = FacesContext.getCurrentInstance();
          ExternalContext externalContext = facesContext.getExternalContext();
          HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
  
          facesContext.responseComplete();
      }
  </code></pre>
  
  <p>}</p>
  
  <p><img src="https://i.stack.imgur.com/EyELm.jpg" alt="JSF Page showing the download link"></p>
  
  <p>When I click on download, I want the downloadPDF() called. But it seems to be calling getArchiveFiles() method again instead of downloadPDF(). I have changed p:CommandLink to the below code, but it still does not call the right method. On top of it I have to pass in the filename parameter as well if I get this working.</p>
  
  <pre><code>&lt;h:commandLink id="downloadLink" value="Download PDF" target="_blank" action="#{pc_ArchiveFiles.downloadPDF}" /&gt;
  </code></pre>