在primefaces中绑定文件下载的参数 [英] bind a parameter for file download in primefaces

查看:92
本文介绍了在primefaces中绑定文件下载的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在primefaces中下载一个名称可能不同的文件。

I would like to download in primefaces a file whose name can vary.

这是控制器的代码

@ManagedBean(name="fileDownloadController", eager = true)
@ViewScoped
public class FileDownloadController implements Serializable{

private StreamedContent file;  

private String fileName;

public FileDownloadController() {   
  System.out.println("FileDownloadController sans arg");
  System.out.println("getFileName:" + fileName);

  InputStream stream = null;  
  try {
     stream = new FileInputStream("D:/myFileDir/"+fileName);
  } catch (FileNotFoundException ex) {
    Logger.getLogger(FileDownloadController.class.getName()).log(Level.SEVERE, null, ex);
   }
   file = new DefaultStreamedContent(stream, "image/jpg", fileName);  
  }
}

这里是xhtml

<c:forEach  items="#{myBean.files}" var="file" >
   <p:row>
      <p:column>
         <p:commandButton id="downloadLink" value="#{file.fileName}" ajax="false" onclick="PrimeFaces.monitorDownload(start, stop)" icon="ui-icon-arrowthichk-s" >  
            <f:setPropertyActionListener target="#{fileDownloadController.fileName}" value="#{file.fileName}"/>
            <p:fileDownload value="#{fileDownloadController.file}" />  
         </p:commandButton>   
     </p:column>
   </p:row>
 </c:forEach>   

问题是在控制器中fileName为null,因此f:setPropertyActionListener未正确配置。
但是,我找不到解决方案。

The problem is that in the controller the fileName is null, hence the f:setPropertyActionListener is not configured properly. However, I can't find a solution.

推荐答案

为什么不直接传递fileName ...

Why not to pass the fileName directly ...

Bean

Bean

@ManagedBean(name="fileDownloadController", eager = true)
@ViewScoped
public class FileDownloadController implements Serializable{

   public StreamedContent generateFile(String fileName) {   
      InputStream stream = null;  
      try {
        stream = new FileInputStream("D:/myFileDir/"+fileName);
      } catch (FileNotFoundException ex) {

      }
      return new DefaultStreamedContent(stream, "image/jpg", fileName);  
   }

}

XHTML

XHTML

<p:commandButton id="downloadLink" value="#{file.fileName}" 
                 ajax="false" onclick="PrimeFaces.monitorDownload(start, stop)" 
                 icon="ui-icon-arrowthichk-s" >
        <p:fileDownload value="#{fileDownloadController.generateFile(file.fileName)}" />  
 </p:commandButton> 

希望这会有所帮助。

这篇关于在primefaces中绑定文件下载的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆