1

私のページで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";
}

しかし、ファイルのダウンロードは機能しません。誰でも助けることができますか?

4

1 に答える 1

4

まだ準備が整っていないp:commandButtonため、アクションメソッド from を先に呼び出す必要があると思います。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>
于 2013-09-05T09:43:32.620 に答える