素面中的多个图像上传 [英] Multiple Image upload in primefaces

查看:38
本文介绍了素面中的多个图像上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有多个上传选项的PrimeFaces fileUpload.在我的项目中,我想在图像上传期间发送电子邮件通知.我的问题是,当我上传10张图片时,意味着同时发送10条电子邮件通知.我只想在上传10张图片时仅发送一封电子邮件通知.我正在使用primefaces 3.0和jsf 2.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?

我的jsf页面:

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

Backing Bean:

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

推荐答案

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

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.

类似这样的东西:

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

这篇关于素面中的多个图像上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆