Muthu
Muthu

Reputation: 279

Multiple Image upload in primefaces

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);

Upvotes: 0

Views: 8233

Answers (1)

BalusC
BalusC

Reputation: 1109282

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}" />

Upvotes: 5

Related Questions