简体   繁体   English

PrimeFaces 3.2简单的FileUpload,在上传之前获取所选文件信息,如文件名

[英]PrimeFaces 3.2 simple FileUpload , Get selected file Info like file name before upload

I am using (Java Server Faces 2.2) Primefaces 3.2 simple file upload controller. 我正在使用(Java Server Faces 2.2)Primefaces 3.2简单文件上传控制器。 I need to access file info before upload. 我需要在上传之前访问文件信息。 What listener can I use when a file is selected & how do i get file info in the ManagedBean before initiating the upload 选择文件时我可以使用什么监听器?如何在启动上载之前在ManagedBean获取文件信息

The tag does not support any ajax behavior event, so the only thing you can do is to call a method before the upload starts using the "onstart" attribute of that fires when upload starts. 标记不支持任何ajax行为事件,因此您唯一能做的就是在上传开始之前使用该触发器“onstart”属性调用方法。 Using a remote command you can do something like: 使用远程命令,您可以执行以下操作:

<p:remoteCommand name="beforeUpdate" partialSubmit="true" process="@this" 
                                 actionListener="#{myBean.doBefore}" value="" />

add call to remote command to the fileUpload 将对远程命令的调用添加到fileUpload

<p:fileUpload fileUploadListener="#{itemImportDialogController.uploadListener}" 
         mode="advanced" multiple="true" onstart="beforeUpdate()"
         styleClass="importItems" update=":itemImportView:fileForm" style="margin: 10px 0"/>

and in the bean add a method like this 并在bean中添加这样的方法

public void doBefore() {
     //DO SOME WORK
}

About file name you can retrieve it only when the file is being uploaded 关于文件名,只有在上传文件时才能检索它

public void uploadListener(FileUploadEvent event) {

    UploadedFile file = event.getFile();
    //DO SOMETHING
}

because there is no ajax interaction possible between the component and the server before this. 因为在此之前组件和服务器之间不存在ajax交互。 So I'm sorry but is not possible. 所以我很抱歉,但不可能。

By the way you could try to manage this through jQuery catching the event with something like 顺便说一下,你可以尝试通过jQuery捕获事件来管理这个

$('input[type=file]').change(function() { 
    //GET THE FILE AND SUBMIT IT TO THE SERVER WITH AJAX CALL 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM