fileDownload primefaces不起作用

时间:2013-09-05 09:29:35

标签: jsf primefaces download

我正在尝试在我的网页上下载pdf文件:

                <p:commandButton action="#{patientCardMB.saveHistoryPdf()}" value="PDF" ajax="false" icon="ui-icon-document" onclick="PrimeFaces.monitorDownload(start, stop)">
                    <p:fileDownload value="#{patientCardMB.file}" />  
                </p:commandButton>

保存历史记录方法:

public String saveHistoryPdf() throws FileNotFoundException {
    ArrayList<PatientCard> patientHistory = (ArrayList) getHistory();
    if (new HistoryPdf().createPdf(patientHistory)) {
        InputStream stream = new FileInputStream("C:\\Users\\XXXX\\Documents\\NetBeansProjects\\Project\\pdf\\" + patientHistory.get(0).getPatientId().getFirstName() + patientHistory.get(0).getPatientId().getLastName() + ".pdf");
        file = new DefaultStreamedContent(stream, "application/pdf", "dsadsaa.pdf");
        sendInfoMessageToUser("Pdf został stworzony");
    } else {
        sendErrorMessageToUser("Podczas tworzenia pliku pdf wystąpił błąd");
    }
    return "pdf";
}

但是文件下载不起作用。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:4)

我认为您需要先调用p:commandButton中的操作方法,因为StreamedContent尚未准备好。您可以使用某种init方法执行此操作,使用actionListener代替action,也可以像这样重写saveHistoryPdf方法:

public StreamedContent saveHistoryPdf() throws FileNotFoundException {
  ArrayList<PatientCard> patientHistory = (ArrayList) getHistory();
  if (new HistoryPdf().createPdf(patientHistory)) {
    InputStream stream = new FileInputStream("C:\\Users\\XXXX\\Documents\\NetBeansProjects\\Project\\pdf\\" + patientHistory.get(0).getPatientId().getFirstName() + patientHistory.get(0).getPatientId().getLastName() + ".pdf");
    file = new DefaultStreamedContent(stream, "application/pdf", "dsadsaa.pdf");
    sendInfoMessageToUser("Pdf został stworzony");
  } else {
    sendErrorMessageToUser("Podczas tworzenia pliku pdf wystąpił błąd");
  }

  return file;
}

因此将xhtml代码更改为:

<p:commandButton value="PDF" ajax="false" icon="ui-icon-document" onclick="PrimeFaces.monitorDownload(start, stop)">
  <p:fileDownload value="#{patientCardMB.saveHistoryPdf}" />  
</p:commandButton>