jsf - Primefaces文件下载不起作用?

标签 jsf primefaces

试图让一个简单的文件下载工作,我得到的只是一个悬挂的 AJAX 状态栏,就是这样。我的支持 bean 输出在准备和下载中呈现正确的名称。

我做错了吗?在我看来,这两个输出都是正确的。

JSF 2.0
Primefaces 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());
}

最佳答案

Primefaces 版本指南 >= 10:
对于 10 之前的 Primefaces 版本,您必须在 commandButton 上禁用 ajax。与 ajax=false .
由于不再需要版本 10,请参阅 Primefaces Documentation 10 .
Primefaces 版本 < 10 的指南:
Primefaces Documentation 6.2

If you’d like to use PrimeFaces commandButton and commandLink, disable ajax option as fileDownload requires a full page refresh to present the file.

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

关于jsf - Primefaces文件下载不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16093527/

相关文章:

java - 输入新条目时需要更新部分页面

jsf - 如何将双击监听器添加到 primefaces 数据表

jsf - Primefaces 文件上传和 session 超时

jsf - 应用 <p :commandButton> style to <p:commandLink> because I basically need <p:commandButton target ="_blank">

primefaces - 使用 primefaces 扩展 (inputNumber)

jsf - 如何更改当前选定菜单项的颜色?

java - Glassfish 3.X,以编程方式处理任意 HTTPSession 的终止

java - 邮件至 : on selected option from selectOneMenu in JSF

jsf-2 - 可编辑数据表中的 SelectBooleanCheckbox 不会改变

javascript - 从 javascript 更新 bean