【问题标题】:PrimeFaces 3.2 simple FileUpload , Get selected file Info like file name before uploadPrimeFaces 3.2 简单 FileUpload ,在上传前获取所选文件信息,如文件名
【发布时间】:2012-12-11 17:03:26
【问题描述】:
我正在使用 (Java Server Faces 2.2) Primefaces 3.2 简单文件上传控制器。我需要在上传之前访问文件信息。选择文件时我可以使用什么监听器以及如何在启动上传之前在ManagedBean 中获取文件信息
【问题讨论】:
标签:
java
file-upload
primefaces
【解决方案1】:
标签不支持任何 ajax 行为事件,所以你唯一能做的就是在上传开始之前调用一个方法,使用上传开始时触发的 "onstart" 属性。
使用远程命令,您可以执行以下操作:
<p:remoteCommand name="beforeUpdate" partialSubmit="true" process="@this"
actionListener="#{myBean.doBefore}" value="" />
将远程命令调用添加到文件上传
<p:fileUpload fileUploadListener="#{itemImportDialogController.uploadListener}"
mode="advanced" multiple="true" onstart="beforeUpdate()"
styleClass="importItems" update=":itemImportView:fileForm" style="margin: 10px 0"/>
并在 bean 中添加这样的方法
public void doBefore() {
//DO SOME WORK
}
关于文件名,只有在文件上传时才能找回
public void uploadListener(FileUploadEvent event) {
UploadedFile file = event.getFile();
//DO SOMETHING
}
因为在此之前组件和服务器之间没有可能的 ajax 交互。
所以很抱歉,但这是不可能的。
顺便说一句,你可以尝试通过 jQuery 来管理这个事件,比如
$('input[type=file]').change(function() {
//GET THE FILE AND SUBMIT IT TO THE SERVER WITH AJAX CALL
});