Single mode allows only one file to be selected at a time from the native file dialog.
<div class="card">
<h:form enctype="multipart/form-data">
<p:fileUpload listener="#{fileUploadView.handleFileUpload}" mode="advanced" dragDropSupport="false"
update="messages" accept=".gif,.jpg,.jpeg,.png"
onupload="return confirm('Are you sure?')">
<p:validateFile sizeLimit="100000" fileLimit="3" allowTypes="/(\.|\/)(gif|jpeg|jpg|png)$/" contentType="true" />
</p:fileUpload>
<p:growl id="messages" showDetail="true"/>
</h:form>
</div>
@Named
@RequestScoped
public class FileUploadView {
private UploadedFile file;
private UploadedFiles files;
private String dropZoneText = "Drop zone p:inputTextarea demo.";
public void upload() {
if (file != null) {
FacesMessage message = new FacesMessage("Successful", file.getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
public void uploadMultiple() {
if (files != null) {
for (UploadedFile f : files.getFiles()) {
FacesMessage message = new FacesMessage("Successful", f.getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
}
public void handleFileUpload(FileUploadEvent event) {
FacesMessage message = new FacesMessage("Successful", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, message);
}
public void handleFileUploadTextarea(FileUploadEvent event) {
String jsVal = "PF('textarea').jq.val";
String fileName = EscapeUtils.forJavaScript(event.getFile().getFileName());
PrimeFaces.current().executeScript(jsVal + "(" + jsVal + "() + '\\n\\n" + fileName + " uploaded.')");
}
public void handleFilesUpload(FilesUploadEvent event) {
for (UploadedFile f : event.getFiles().getFiles()) {
FacesMessage message = new FacesMessage("Successful", f.getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public UploadedFiles getFiles() {
return files;
}
public void setFiles(UploadedFiles files) {
this.files = files;
}
public String getDropZoneText() {
return dropZoneText;
}
public void setDropZoneText(String dropZoneText) {
this.dropZoneText = dropZoneText;
}
}