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?
To handle file uploads with AJAX and store the files on a backend server (e,g PHP Server), create an HTML form and two upload scripts: one written in JavaScript and the other in PHP.: HTML form In your root directory, build an HTML form (an index.html file) with the following code, which contains the fields for file uploads:
Mode of the fileupload, can be simple or advanced. Label of the upload button. Label of the cancel button. Message to display when size limit exceeds. Message to display when file is not accepted. Message to display when file limit exceeds.
Primefaces has removed that burden by providing you a ready-made FileUpload component that help you in creating beautiful UI with backend support for upload files to the server. We will look into the Primefaces FileUpload component features that you can use in your application.
This article describes how to upload AJAX files with Cloudinary with only a few lines of code and no need for any of the above tasks. As a first step, create a free Cloudinary account, which includes a dashboard, a unique cloud name for you, an API key, and an API secret, which you’ll need to work with Cloudinary.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With