8

I want to upload a file with use of PrimeFaces and a ManagedBean. I want use p:fileUpload with mode="simple".

XHTML Code:

    <p:fileUpload id="fileId" mode="simple" value="#{itemBean.upFile}"
        fileLimit="1" />
    <p:commandButton ajax="true" value="Upload File" update="messagess"
                     id="save-btn"
                     actionListener="#{itemBean.fileUpload(itemBean.upFile,itemBean.hiddenFileName)}"
                     process="@this" oncomplete="showImage()" />

ManagedBean:

public void fileUpload(UploadedFile uploadFile, String hiddenKey) {  
    String keyFileName = hiddenKey;

    boolean validFile = true;
    String expression = "([^\\s]+(\\.(?i)(gif|jpg|jpeg|gif|png|PNG|GIF|JPG|JPEG|bmp))$)";
    if((uploadFile == null) ) {
        validFile = false;
        FacesMessage msg = new FacesMessage("Error! "+ "Please select an image.");  
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }  
    else {
    System.out.println("going to file upload"+uploadFile.getFileName()+"---hiddenKey"+keyFileName);
    if((!uploadFile.getFileName().matches(expression)) ) {
        validFile = false;
        FacesMessage msg = new FacesMessage("Error! "+ uploadFile.getFileName() + " is not an image.");  
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    if(uploadFile.getSize() > 1000000) {
        validFile = false;
        FacesMessage msg = new FacesMessage("Error! "+ uploadFile.getFileName() + " size is too large.");  
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    if (validFile) {
        // Do what you want with the file        
        try {
            //String extn =uploadFile.getFileName().substring(uploadFile.getFileName().lastIndexOf("."));


            copyFile(uploadFile.getFileName(), uploadFile.getInputstream());

            FacesMessage msg = new FacesMessage("Success! "+ uploadFile.getFileName() + " is uploaded.");  
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } catch (IOException e) {
            e.printStackTrace();
            FacesMessage msg = new FacesMessage("Error! "+ uploadFile.getFileName()+ " not uploaded.");  
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
    }
}  

My problem is, that I clicked on the command button and method in the backing bean is not called. If I use ajax="false" the method is called, but the page got refreshed.

How can I use ajax="true" and <p:fileUpload> together?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user2981922
  • 91
  • 1
  • 1
  • 2
  • You don't need to explicitly set `ajax=true` on `commandButton`. It's by default `ajax=true` even you set or not.Try removing `ajax=true`. And `actionListner` needs to be associate to `ActionEvent` – SRy Nov 15 '13 at 14:55

1 Answers1

10

The <p:fileUpload mode="simple"> doesn't support ajax. Sorry, but this is end of story.

If switching to the ajax-compatible <p:fileUpload mode="advanced"> is really not an option, then your best bet is upgrading to JSF 2.2 and use its new native <h:inputFile> component instead. It also shows up in browser default look'n'feel and is capable of simulating the ajax experience via a hidden iframe trick.

See also:


Unrelated to the concrete problem, those two arguments in your fileUpload() action method are completely unnecessary. Just the

action="#{itemBean.fileUpload}"

with

public void fileUpload() {
    // ...
}

works as good once you remove process="@this" attribute.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 4
    simulating the ajax experience via a hidden iframe trick? Could you explain this? thanks – Dave May 27 '14 at 15:48