title: PrimeFaces fileDownload method not called?
body: <p>I've got multiple pages that allow the download of the same resource (retrieved from my DB).</p>
  
  <p>The problem is that the download works just on some of them, even with the SAME code and calling the SAME bean.</p>
  
  <p>This thing is getting quite annoying because, on the non working pages, clicking on the download link will just reload the page without any message/exception, so I can't find out what's happening.</p>
  
  <p>Here's my BEAN code :</p>
  
  <pre><code>package ManagedBeans;
  
  import ejb.DispensaManagerLocal;
  import entity.Dispensa;
  import entity.Utente;
  import java.io.ByteArrayInputStream;
  import javax.ejb.EJB;
  import javax.faces.application.FacesMessage;
  import javax.faces.bean.ManagedBean;
  import javax.faces.bean.ManagedProperty;
  import javax.faces.bean.RequestScoped;
  import javax.faces.context.FacesContext;
  import org.primefaces.event.RateEvent;
  import org.primefaces.model.DefaultStreamedContent;
  import org.primefaces.model.StreamedContent;
  
  /**
   *
   * @author stefano
   */
  @ManagedBean
  @RequestScoped
  public class DispensaBean {
  
      @EJB
      private DispensaManagerLocal dispensaManager;
      @ManagedProperty(value = "#{loginBean.utente}")
      private Utente utente;
  
      public Utente getUtente() {
          return utente;
      }
  
      public void setUtente(Utente utente) {
          this.utente = utente;
      }
  
      /**
       * Creates a new instance of DispensaBean
       */
      public DispensaBean() {
      }
  
      public StreamedContent getDownload() {
          String id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("dispensaId");        
          System.out.println("________" + id);
          Dispensa d = dispensaManager.findById(Integer.parseInt(id));        
          String type = getMimeFromByte(d.getDatiFile());
          String estensione = "";
          if(type.equals("application/pdf")){
              estensione = ".pdf";
          } else if(type.equals("application/zip")) {
              estensione = ".zip";
          } else if(type.equals("application/vnd.ms-powerpoint")) {
              estensione = ".ppt";
          }        
          return new DefaultStreamedContent(new ByteArrayInputStream(d.getDatiFile()), type, d.getTitolo() + estensione);
      }
  
  
      private String getMimeFromByte(byte[] src) {
          if (src[0] == 0x25 &amp;&amp; src[1] == 0x50 &amp;&amp; src[2] == 0x44 &amp;&amp; src[3] == 0x46) {
              return "application/pdf";
          }
          if (src[0] == 0x50 &amp;&amp; src[1] == 0x4b) {
              return "application/zip";
          }
          if (src[0] == 0xd0 &amp;&amp; src[1] == 0xcf &amp;&amp; src[2] == 0x11 &amp;&amp; src[3] == 0xe0 &amp;&amp; src[4] == 0xa1 &amp;&amp; src[5] == 0xb1 &amp;&amp; src[6] == 0x1a &amp;&amp; src[7] == 0xe1) {
              return "application/vnd.ms-powerpoint";
          }
          return "application/octet-stream";
      }
  
  }
  </code></pre>
  
  <p>Now, on the NON working pages, the <code>getDownload()</code> method is NOT called, as it doesn't print anything.</p>
  
  <p>Here's the download button code</p>
  
  <pre><code>&lt;h:form style="float: right"&gt;
      &lt;pou:commandLink id="downloadDispensa" ajax="false" disabled="#{!loginBean.logged}"&gt;
         &lt;pou:graphicImage value="./resources/images/download.png" height="30"/&gt;
         &lt;pou:fileDownload value="#{dispensaBean.getDownload()}"/&gt;                                                    
         &lt;f:param name="dispensaId" value="#{dispensa.id}"/&gt;
      &lt;/pou:commandLink&gt;                                
  &lt;/h:form&gt; 
  </code></pre>
  
  <p>What I've noticed is that the download link just RELOADS the page instead of calling the method, and this happens only in the pages in which <code>#{dispensa.id}</code> depends on a GET parameter.</p>
  
  <p>For example, I've got a page called <code>dispensa.xhtml</code> that displays all my files in the DB if no GET parameters are passed.</p>
  
  <p>Indeed, <code>dispensa.xhtml?id=5</code> will display just the file with id=5.</p>
  
  <p>Clicking on the download link, in the first case, works without problems.
  Doing it in the second case will reload the page and will lose the GET parameter, so the it will load <code>dispensa.xhtml</code> instead of <code>dispensa.xhtml?id=5</code>.</p>
  
  <p>I'd think that there's some problem in using the GET parameter, but..yesterday it WORKED and I did NOT change this code!</p>
  
  <p>The other NON working page is <code>ricerca.xhtml</code> which shows the (multiple) results of a query given by <code>ricerca.xhtml?key=query</code>.</p>
  
  <p>Finally, to mess things up, the download in <code>profile.xhtml?user=username</code> WORKS.</p>
  
  <p>This destroys my whole theory about GET parameters.</p>
  
  <p>To avoid having a null <code>byte[] datiFile</code>, I'ved edited my <code>Dispensa</code> entity this way :</p>
  
  <pre><code>@Basic(optional = true, fetch=FetchType.EAGER)
  @Lob
  @Column(name = "datiFile")    
  private byte[] datiFile;
  </code></pre>
  
  <p>I don't know what to do because it doesn't say what's going wrong, it just reloads the page, bypassing my download!</p>
  
  <p>EDIT :</p>
  
  <p>I've tried changing my <code>getDownload()</code> method to return a <code>File</code> which is on my HD, to understand if the problem is caused by a null data on the db but it still doesn't work as I said!</p>