Jsf 文件下载primefaces不起作用
我正在尝试在我的页面上下载pdf文件:Jsf 文件下载primefaces不起作用,jsf,primefaces,download,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="#{
<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";
}
但是文件下载不起作用。有人能帮忙吗?我认为需要提前调用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>
我认为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>
服务器日志文件/客户端firebug中的错误,控制台端?服务器日志文件/客户端firebug中的错误,控制台端?谢谢您的回答!我也有同样的问题,我只是忘记了ajax='false'。。。希望这能帮助人们解决同样的问题:他们的关键是,在PrimeFacesShowcase中使用FacesContext。。。但在不同的环境中,您需要使用FileInputStream。谢谢您的回答!我也有同样的问题,我只是忘记了ajax='false'。。。希望这能帮助人们解决同样的问题:他们的关键是,在PrimeFacesShowcase中使用FacesContext。。。但在不同的环境中,您需要使用FileInputStream。