p:fileUpload oncomplete被执行多次

时间:2017-11-13 14:53:41

标签: jsf file-upload primefaces

我希望我的用户能够上传一到四张图片。

然后,这些图像将附加到单个电子邮件中,该文件将在文件上传完成后构建并发送。

在将所有文件附加到电子邮件之前,无法发送电子邮件。

我在多种模式下使用fileUpload组件来从用户那里获取文件,我希望我可以使用fileUpload组件中的oncomplete属性来执行此操作,但似乎已为每个文件调用此文件选择,而不是在整个列表上传时。

当所有文件完成上传后,有没有办法只进行一次回调?我似乎无法找到一个。

标记:

            <p:fileUpload id = "fileChooser"
                          label="Choose File"
                          uploadLabel="Upload File(s)"
                          cancelLabel="Start Again" 
                          skinSimple="true"
                          fileUploadListener = "#{MyController.handleFileUpload}"
                          mode = "advanced"                           
                          dragDropSupport = "false"
                          multiple = "true"
                          update = "@this"
                          sizeLimit = "4000000"                           
                          fileLimit = "4" 
                          allowTypes = "/(\.|\/)(gif|jpe?g|png)$/"
                          oncomplete="#{MyController.uploadCompleted()}" />

豆:

@Named
@SessionScoped
public class MyBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private List<UploadedFile> uploadedFiles;

    @PostConstruct
    public void init() {
        uploadedFiles = new ArrayList<UploadedFile>();
    }   

    public void upload(FileUploadEvent event) {     
        uploadedFiles.add(event.getFile());
    }    

    public List<UploadedFile> getUploadedFiles() {
        return uploadedFiles;
    }   
    public void setUploadedFiles(List<UploadedFile> uploadedFiles) {
        this.uploadedFiles = uploadedFiles;
    }

}

控制器:

@Named
@SessionScoped
public class MyController implements Serializable {

    private static final long serialVersionUID = -261758226698276865L;

    private static final Logger LOG = Logger.getLogger(MyController.class);

    @Inject
    private MyBean myBean; 

    // This is hit every single time a file finishes uploading which is unwanted behaviour.
    public void uploadCompleted() {
        LOG.info("Uploading finished, Number of files:" + myBean.getUploadedFiles().size());
    }

    public void handleFileUpload(FileUploadEvent event) {
        LOG.info("New file upload event passed to event handler");
        LOG.info("File Name :" + event.getFile().getFileName() + " File Size:" + event.getFile().getSize());
        myBean.upload(event);       
    }

    public MyBean getMyBean() {
        return uploadNonUKLicenceBean;
    }
    public void setMyBean(MyBean myBean) {
        this.myBean = myBean;
    }

}

1 个答案:

答案 0 :(得分:2)

如果有其他人遇到这个,当选择文件时,它们被添加到fileUpload files属性中包含的列表中,因为文件被上传然后它们被删除,所以使用p:remoteCommand我刚检查了这个的长度,以便我可以在控制器空的时候调用我想要的方法。

(我的原始问题中有一个错误,其中oncomplete属性在我清理代码时会在那里悄悄地发布,我在答案中根据评论更正了。)

如果有更好的方法,我很乐意知道。

<p:fileUpload id = "fileChooser"
              label="Choose File"
              uploadLabel="Upload File(s)"
              cancelLabel="Start Again" 
              fileUploadListener = "#{nonUKLicenceCheckController.handleFileUpload}"
              mode = "advanced"                           
              dragDropSupport = "false"
              multiple = "true"
              update = "@this"
              sizeLimit = "4000000"                           
              fileLimit = "4" 
              allowTypes = "/(\.|\/)(gif|jpe?g|png)$/"
              widgetVar="uploadWidget"
              oncomplete="isFileListEmpty(PF('uploadWidget'), uploadsFinished);"
              />            
<p:remoteCommand name="uploadsFinished" actionListener="#{nonUKLicenceCheckController.uploadCompleted()}" />
<script type="text/javascript">
    function isFileListEmpty(fileupload, finished) {
    if (fileupload.files.length === 0) {
            if (finished) {
                finished();
            }
        }
    }
</script>