title: How to download files in the filtered datatable of Primefaces?
body: <p>I have a datatable which allows you to download files. It works. However when I try to download a file after filtering returned is first on the list.</p>
  
  <p>This image shows that when clicked on teste4.txt the same file was downloaded. This is correct. <img src="https://i.stack.imgur.com/kSp3T.png" alt="Correct download"></p>
  
  <p>This other picture shows that when clicked on file teste4.txt, after filtering, the downloaded file was teste1.txt <img src="https://i.stack.imgur.com/Omb5x.png" alt="Download wrong"></p>
  
  <p>This is my file containing the datatable:
  <code></p>
  
  <pre><code>&lt;h:form id="hFormListaArquivosRegiao2" enctype="multipart/form-data"&gt;
  
      &lt;p:dataTable id="pDataTableListaArquivos" var="arquivo" value="#{arquivoBean.listaArquivos}" filteredValue="#{arquivoBean.filteredListaArquivos}"&gt;
  
          &lt;p:column id="pColumnNomeArquivo" headerText="#{msg.NomeDoArquivo}" sortBy="#{arquivo.nomeArquivo}" filterMatchMode="contains" filterBy="#{arquivo.nomeArquivo}"&gt;
  
              &lt;h:commandLink action="#{arquivoBean.download}" title="#{arquivo.nomeArquivo}"&gt;
  
                  &lt;h:outputText value="#{arquivo.nomeArquivo}" /&gt;
                  &lt;f:setPropertyActionListener target="#{arquivoBean.arquivo}" value="#{arquivo}" /&gt;
  
              &lt;/h:commandLink&gt;
  
          &lt;/p:column&gt;
  
  &lt;/h:form&gt;
  </code></pre>
  
  <p></code></p>
  
  <p>This is the method of backingBean responsible for downloading:
  <code></p>
  
  <pre><code>public void download() throws Exception {
  
      logger.debug("NOME ARQUIVO: "+ ContextoBean.CAMINHO_ARQUIVOS + arquivo.getNomeArquivo() +", "+ arquivo.getNomeArquivo());
  
      FacesContext facesContext = FacesContext.getCurrentInstance();
      pushArquivo(facesContext.getExternalContext(), ContextoBean.CAMINHO_ARQUIVOS + arquivo.getNomeArquivo(), arquivo.getNomeArquivo());
      facesContext.responseComplete();
  
  }
  </code></pre>
  
  <p></code></p>
  
  <p>This is the helper method of backingBean responsible for downloading:
  <code></p>
  
  <pre><code>private void pushArquivo(ExternalContext externalContext, String nomeArquivo, String nomeDeExibicao) throws IOException {
  
      // Info sobre a lógica usada: http://stackoverflow.com/questions/1686821/execute-backing-bean-action-on-load
      File descritoArquivo = new File(nomeArquivo);
      int length = 0;
      OutputStream os = externalContext.getResponseOutputStream();
      String extencaoArquivo = externalContext.getMimeType(nomeArquivo);
      externalContext.setResponseContentType((extencaoArquivo != null) ? extencaoArquivo : "application/octet-stream");
      externalContext.setResponseContentLength((int) descritoArquivo.length());
      externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"" + nomeDeExibicao + "\"");
  
      // Stream to the requester.
      byte[] bbuf = new byte[1024];
      DataInputStream in = new DataInputStream(new FileInputStream(descritoArquivo));
      while ((in != null) &amp;&amp; ((length = in.read(bbuf)) != -1)) {
          os.write(bbuf, 0, length);
      }
  
      in.close();
  
  }
  </code></pre>
  
  <p></code></p>