2012-05-12 33 views
3

我有多個頁面允許下載相同的資源(從我的數據庫中檢索)。PrimeFaces fileDownload方法未被調用?

問題是,即使使用SAME代碼和調用SAME bean,下載也只能在其中一些上進行。

這件事情變得非常煩人,因爲在非工作頁面上,單擊下載鏈接將只是重新加載頁面而沒有任何消息/異常,所以我無法找出發生了什麼。

這裏是我的BEAN代碼:

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 && src[1] == 0x50 && src[2] == 0x44 && src[3] == 0x46) { 
      return "application/pdf"; 
     } 
     if (src[0] == 0x50 && src[1] == 0x4b) { 
      return "application/zip"; 
     } 
     if (src[0] == 0xd0 && src[1] == 0xcf && src[2] == 0x11 && src[3] == 0xe0 && src[4] == 0xa1 && src[5] == 0xb1 && src[6] == 0x1a && src[7] == 0xe1) { 
      return "application/vnd.ms-powerpoint"; 
     } 
     return "application/octet-stream"; 
    } 

} 

現在,在非工作頁,getDownload()方法不叫,因爲它不顯示任何信息。

這裏的下載按鈕代碼

<h:form style="float: right"> 
    <pou:commandLink id="downloadDispensa" ajax="false" disabled="#{!loginBean.logged}"> 
     <pou:graphicImage value="./resources/images/download.png" height="30"/> 
     <pou:fileDownload value="#{dispensaBean.getDownload()}"/>              
     <f:param name="dispensaId" value="#{dispensa.id}"/> 
    </pou:commandLink>         
</h:form> 

我已經注意到的是,下載鏈接只是RELOADS而不是調用此方法的頁面,而這只是發生在其中#{dispensa.id}取決於GET頁面參數。

例如,我有一個名爲dispensa.xhtml的頁面,如果沒有傳遞GET參數,它會在數據庫中顯示我的所有文件。

事實上,dispensa.xhtml?id=5將只顯示id = 5的文件。

點擊下載鏈接,在第一種情況下,工作沒有問題。 在第二種情況下執行此操作將重新加載頁面,並且將丟失GET參數,因此它將加載dispensa.xhtml而不是dispensa.xhtml?id=5

我認爲在使用GET參數時存在一些問題,但是......昨天它工作了,我沒有改變這個代碼!

另一個非工作頁面是ricerca.xhtml,它顯示ricerca.xhtml?key=query給出查詢的(多個)結果。

最後,搞砸了,下載profile.xhtml?user=username WORKS。

這破壞了我關於GET參數的全部理論。

爲了避免空byte[] datiFile,I'ved編輯我Dispensa實體是這樣的:

@Basic(optional = true, fetch=FetchType.EAGER) 
@Lob 
@Column(name = "datiFile")  
private byte[] datiFile; 

我不知道該怎麼辦,因爲它並沒有說什麼錯,它只是重新加載頁面,繞過我的下載!

編輯:

我試圖改變我的getDownload()方法返回一個File這是對我的房屋,瞭解問題是否通過在一個數據庫中的空數據引起的,但它仍然無法正常工作我說!

+0

您應該將解決方案作爲答案發布,而不是在問題中(而不是在標題中放置已解決,但只是將其標記爲已接受)。堆棧溢出是一個問答網站,而不是論壇。 – BalusC

+0

從來沒有想過我可以接受我自己的答案! – StepTNT

+1

你只得不到積分:) – BalusC

回答

3

似乎我通過使用替代解決方案解決了這個問題。

我已經改變了所有

<h:form style="float: right"> 
     <pou:commandLink id="downloadDispensa" ajax="false" disabled="#{!loginBean.logged}"> 
      <pou:graphicImage value="./resources/images/download.png" height="30"/> 
      <pou:fileDownload value="#{dispensaBean.getDownload()}"/>              
      <f:param name="dispensaId" value="#{dispensa.id}"/> 
     </pou:commandLink>         
    </h:form> 

<h:form style="float: right"> 
    <h:outputLink id="downloadDispensa" disabled="#{!loginBean.logged}" target="_blank" value="./download.xhtml?id=#{dispensa.id}"> 
     <pou:graphicImage value="./resources/images/download.png" height="30"/>          
    </h:outputLink>         
</h:form> 

其中download.xhtml有這樣的代碼:

<script type="text/javascript"> 
    if(document.referrer == "" || document.referrer == "download.xhtml"){ 
     self.location='./index.xhtml'; 
    } 
    document.onblur = new Function('self.close()'); 
</script> 
<h:body onload="document.getElementsByClassName('downloadDispensa')[0].click();" rendered="#{loginBean.logged}"> 
    <h:form>    
     <h:commandLink class="downloadDispensa" id="downloadDispensa" style="display: none">     
      <pou:graphicImage value="./resources/images/download.png" height="30"/> 
      <pou:fileDownload value="#{dispensaBean.download}"/>                      
      <f:param name="dispensaId" value="#{request.getParameter('id')}"/> 
     </h:commandLink> 
    </h:form>   
</h:body> 
<h:body onload="self.location='./index.xhtml';" rendered="#{!loginBean.logged}"> 
</h:body> 

所以它加載的下載頁面,autoclicks的下載鏈接,它會在顯示下載對話框時自動關閉頁面。