Simple uploader uses native browser file upload. Simple uploader is disabled at online demo.
<p:growl id="messages" showDetail="true"/>
<div class="card">
<h5>Single</h5>
<h:form enctype="multipart/form-data">
<p:fileUpload value="#{fileUploadView.file}" mode="simple" skinSimple="true"/>
<p:commandButton value="Submit" ajax="false" action="#{fileUploadView.upload}" styleClass="mt-3 ui-button-outlined block"/>
</h:form>
</div>
<div class="card">
<h5>Single (allowTypes validation)</h5>
<h:form enctype="multipart/form-data">
<p:fileUpload value="#{fileUploadView.file}" mode="simple" skinSimple="true">
<p:validateFile allowTypes="/(\.|\/)(pdf)$/" />
</p:fileUpload>
<p:commandButton value="Submit" action="#{fileUploadView.upload}" styleClass="mt-3 ui-button-outlined block"
validateClient="false" process="@form" update="messages"/>
<p:commandButton value="Submit (+ validateClient)" action="#{fileUploadView.upload}" styleClass="mt-3 ui-button-outlined block"
validateClient="true" process="@form" update="messages"/>
</h:form>
</div>
<div class="card">
<h5>Multiple</h5>
<h:form enctype="multipart/form-data">
<p:fileUpload value="#{fileUploadView.files}" mode="simple" skinSimple="true" multiple="true"/>
<p:commandButton value="Submit" ajax="false" action="#{fileUploadView.uploadMultiple}" styleClass="mt-3 ui-button-outlined block"/>
</h:form>
</div>
package org.primefaces.showcase.view.file;
import jakarta.enterprise.context.RequestScoped;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.context.FacesContext;
import jakarta.inject.Named;
import io.quarkus.runtime.annotations.RegisterForReflection;
import org.primefaces.PrimeFaces;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.event.FilesUploadEvent;
import org.primefaces.model.file.UploadedFile;
import org.primefaces.model.file.UploadedFiles;
import org.primefaces.util.EscapeUtils;
@Named
@RequestScoped
@RegisterForReflection(serialization = true)
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;
}
}