0

I am using PrimeFaces fileUpload with multiple upload options. In my project i want to send email notification during image upload. My problem is when i upload 10 images means simultaneously 10 email notifications are send. I want to send only one email notification during uploading 10 images. I am using primefaces 3.0 and jsf 2.0. How can I solve it?

My jsf pages:

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

Backing Bean:

    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);
Muthu
  • 269
  • 3
  • 9
  • 23
  • don't include the email logic in the handleFileUpload event. make the bean viewscoped and send one single email when the form is submitted. you should have a list of uploaded images in the bean, so yo'll know how many where uploaded. – damian Jun 19 '12 at 12:17

1 Answers1

5

Redesign your bean as such that the file upload handler method merely captures and remembers all uploaded files in some collection. Then add a "Save" button below the form which is bound to an action method which will actually process and save all those uploaded files and finally send the mail. If you put the bean in the view scope, then one and same bean instance will just be reused as long as the enduser interacts with the same view. You could then just collect the uploaded files in a collection property.

Something like this:

@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.
    }

}

with

<p:fileUpload ... fileUploadListener="#{bean.upload}" />
<p:commandButton value="Save" action="#{bean.save}" />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • But i am using in advanced mode.This is my jsf ui pages. – Muthu Jun 20 '12 at 05:10
  • Yes, you have told that. What exactly is your problem now? Don't you know how to add a button? – BalusC Jun 20 '12 at 10:45
  • Ya ok.But i have put a email notification method inside the handleImage method,i have upload 5 images means simultaneously it will uploaded and sending 5 email – Muthu Jun 20 '12 at 10:54
  • Yes, I understand your problem. You want to send only one mail once all images are been uploaded. I have already answered how to solve that. What exactly is your problem now? How exactly is the answer unclear/insufficient? How exactly did you fail to implement the answer accordingly? – BalusC Jun 20 '12 at 10:55
  • ok how to add a button in advanced mode using p:fileUpload tag with multiple option – Muthu Jun 20 '12 at 10:58
  • I updated the answer with a more concrete example of what I tried to explain – BalusC Jun 20 '12 at 11:17
  • The advance mode has its own button. Take a look at this [post](http://stackoverflow.com/questions/26551669/detect-when-the-multiupload-primefaces-componet-ends-all-the-uploads) – angeldev Aug 05 '16 at 09:49