how to pass the parameter during file upload in primefaces [duplicate]

一笑奈何 提交于 2020-01-02 07:41:03

问题


I am using jsf2.0 with primfaces and using p:fileupload for uploading the photo..Here i need to pass the parameter in backing bean. In that there is no option for passing parameter through p:fileupload.. i was used the binding option also,but it returns the null value on backing bean...

here is my fileupload.xhtml

   <h:form  enctype="multipart/form-data">
            <p:panel  header="Upload Photos" id="getImageId" 
             style="font-size:12px;height:499px">
            <p:messages id="messages" for="imaload"></p:messages>
                 <p:fileUpload id="imaload" fileUploadListener="#  
       {ngoPhotoUpload.photoUpload}"  
                           mode="advanced"  multiple="true"   
     immediate="true"
                          update="messages" 
                           allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>

                 <!--  <p:growl life="1000" id="messages"/>   -->

            </p:panel>
        </h:form>

here is my backing bean method

public void photoUpload(FileUploadEvent event) throws IOException,
        InterruptedException {

    BufferedImage bufferedImage;
    String tmpFile = scontext.getRealPath(("/ngoPhotos/")
            + event.getFile().getFileName());
    File result = new File(tmpFile);

    byte[] imageByte = event.getFile().getContents();

    storeImage(imageByte, tmpFile);

    String imageName = event.getFile().getFileName();
    String createdby = loginBean.getEmail();

    if (loginBean.getType().equals("admin")
            || loginBean.getType().equals("ngo_coordinator")) {
        if (ngoRegnPojo.getNgo_id() != 0) {
            ngoPhotoBean.setNgo_id(ngoRegnPojo.getNgo_id());
        } else {
            ngoPhotoBean.setNgo_id(loginBean.getUser_id());
        }
    } else {
        ngoPhotoBean.setNgo_id(loginBean.getUser_id());
    }

    ngoPhotoBean.setImageName(imageName);
    ngoPhotoBean.status = "status";
    ngoPhotoBean.setCreatedDate(new Date());
    ngoPhotoBean.setCreatedby(createdby);
    //ngoPhotoBean.setPathName(tmpFile);
    ngoPhotoBean.disable = "false";
    bufferedImage = ImageIO.read(result);
    if (bufferedImage.getWidth() <= 400 && bufferedImage.getHeight() <= 400) {

        ngoPhotoBean.disable = "false";
        getMthd(imageByte, tmpFile);

    } else {

        try {

            ngoPhotoBean.disable = "false";
            bufferedImage = ImageIO.read(result);
            ImageIO.write(resize(bufferedImage, 400, 400), "jpg", new File(
                    tmpFile));
            photoUploadDaoService.uploadNgoPhoto(ngoPhotoBean);
            NgoPhotoBean ngoPhotoBean = new NgoPhotoBean();
            FacesContext.getCurrentInstance().getExternalContext()
                    .getSessionMap().put("ngoPhotoBean", ngoPhotoBean);
            FacesMessage msg = new FacesMessage("Successfully Uploaded");

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

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

Thanks in advance


回答1:


What kind of parameter you need to pass?If it's just some sort of constant, you put a Hidden field into your form so can receive it's value after submit.




回答2:


You can use a p:remotecommand, for example with the oncomplete of the fileupload you can call the p:remote command and call a function passing the parameter. I did something similar in my proyect and it work pretty good.



来源:https://stackoverflow.com/questions/10909492/how-to-pass-the-parameter-during-file-upload-in-primefaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!