0

我正在使用带有多个上传选项的 PrimeFaces fileUpload。在我的项目中,我想在图像上传期间发送电子邮件通知。我的问题是当我上传 10 张图片时,意味着同时发送了 10 封电子邮件通知。我想在上传 10 张图片时只发送一封电子邮件通知。我正在使用 primefaces 3.0 和 jsf 2.0。我该如何解决?

我的jsf页面:

     <p:fileUpload id="imaload" fileUploadListener="#{photoUploadAction.handleImage}"  
                           mode="advanced"  multiple="true" process="@form" 
                          update="messages,@form" 
                           allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>  

支持豆:

    public void handleImage(FileUploadEvent event) throws IOException, EmailException {
       try {
            photoUploadVO.setDisabled("false");

            //BufferedImage image = ImageIO.read(in);
            ImageIO.write(resize(bufferedImage, 400,  bufferedImage.getHeight()), "jpg", new File(tmpFile));
            flag = photoUploadDaoService.uploadPhotos(photoUploadVO);

            // profileImageService.uploadPhotos(profileImageBean);
            if (flag == true) {

                if(!loginBean.getType().equals("ngo") && !loginBean.getType().equals("admin") &&
                         !loginBean.getType().equals("ngo_coordinator") ){

                     volName = getVolunteerName(photoUploadVO.getUsrId(),photoUploadVO.getUser_type());

                 lst = apDao.retreiveSetup();
                   notification = lst.get(0).activity_email.toString();
                    email = lst.get(0).approval_toEmail.toString();

                    if(notification.equalsIgnoreCase(tmp)){
                          ecs.sendPhotoNotiFication(email,photoUploadVO,volName);
                    }
                 }

                FacesMessage msg = new FacesMessage("Successfully Uploaded");

                FacesContext.getCurrentInstance().addMessage(null, msg);
            } else {
                FacesMessage msg = new FacesMessage("Failure", event
                        .getFile().getFileName() + " to uploaded.");

                FacesContext.getCurrentInstance().addMessage(null, msg);
            }

        } catch (IOException e) {
            e.printStackTrace();

            FacesMessage error = new FacesMessage(
                    FacesMessage.SEVERITY_ERROR,
                    "The files were not uploaded!", "");
            FacesContext.getCurrentInstance().addMessage(null, error);
        }
    }

    This is my email notification method inside handle upload methos:

     ecs.sendPhotoNotiFication(email,photoUploadVO,volName);
4

1 回答 1

5

重新设计您的 bean,以便文件上传处理程序方法仅捕获并记住某个集合中的所有上传文件。然后在表单下方添加一个“保存”按钮,该按钮绑定到一个操作方法,该操作方法将实际处理并保存所有上传的文件并最终发送邮件。如果您将 bean 放在视图范围内,那么只要最终用户与同一个视图交互,就会重用一个相同的 bean 实例。然后,您可以在集合属性中收集上传的文件。

像这样的东西:

@ManagedBean
@ViewScoped
public class Bean implements Serializable {

    private List<UploadedFile> uploadedFiles;

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

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

    public void save() {
        for (UploadedFile uploadedFile : uploadedFiles) {
            // Process them all here.
        }

        // Send only one email.
    }

}

<p:fileUpload ... fileUploadListener="#{bean.upload}" />
<p:commandButton value="Save" action="#{bean.save}" />
于 2012-06-19T13:59:32.613 回答