17

単純なファイルのダウンロードを機能させようとしていますが、AJAX ステータス バーがハングしているだけです。バッキング Bean の出力は、準備とダウンロードで正しい名前をレンダリングします。

私はこれを間違っていますか?両方の出力が正しいようです。

JSF 2.0 プライムフェイス 3.4

        <h:form>
            <p:commandButton id="downloadLink" value="Download" actionListener="#{filemanagement.prepDownload}"> 
                <p:fileDownload value="#{filemanagement.download}" />
            </p:commandButton>
        </h:form>

バッキング Bean:

private DefaultStreamedContent download;

public void setDownload(DefaultStreamedContent download) {
    this.download = download;
}

public DefaultStreamedContent getDownload() throws Exception {
    System.out.println("GET = " + download.getName());
    return download;
}

public void prepDownload() throws Exception {
    File file = new File("C:\\file.csv");
    InputStream input = new FileInputStream(file);
    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    setDownload(new DefaultStreamedContent(input, externalContext.getMimeType(file.getName()), file.getName()));
    System.out.println("PREP = " + download.getName());
}
4

4 に答える 4

31

Primefaces バージョン >= 10 のガイド:

10 より前のバージョンの Primefaces では、commandButtonwith でajax を無効にする必要がありましたajax=false

不要になったバージョン 10 以降については、Primefaces Documentation 10を参照してください。

Primefaces バージョン < 10 のガイド:

Primefaces ドキュメント 6.2を参照してください。

PrimeFaces commandButton と commandLink を使用する場合は、fileDownload でファイルを表示するためにページ全体を更新する必要があるため、ajax オプションを無効にします。

<h:form>
  <p:commandButton id="downloadLink" value="Download" ajax="false" actionListener="#{filemanagement.prepDownload}">
    <p:fileDownload value="#{filemanagement.download}" />
  </p:commandButton>
</h:form>
于 2013-04-18T21:40:05.027 に答える
15

コマンド ボタン内で ajax=false を設定し、コマンドリンクにアクションまたはアクション リスナーを使用しないでください。

<h:form>
  <p:commandButton id="downloadLink" value="Download" ajax="false">
    <p:fileDownload value="#{filemanagement.downloadValue}" />
  </p:commandButton>
</h:form>

豆:

public StreamedContent getDownloadValue() throws Exception {
    StreamedContent download=new DefaultStreamedContent();
    File file = new File("C:\\file.csv");
    InputStream input = new FileInputStream(file);
    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    download = new DefaultStreamedContent(input, externalContext.getMimeType(file.getName()), file.getName()));
    System.out.println("PREP = " + download.getName());
    return download;
}
于 2014-07-23T08:53:19.557 に答える