p:fileUpload oncomplete被执行多次

问题描述 投票:2回答:1

我想让我的用户能够上传一到四张图片。

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

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

我在多个模式下使用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;
    }

}
jsf file-upload primefaces
1个回答
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>
© www.soinside.com 2019 - 2024. All rights reserved.