Simple uploader uses native browser file upload. When 'auto' is set to to 'true', AJAX is used to automatically upload it.
<p:growl id="messages" showDetail="true"/>
<div class="card">
<h5>Single</h5>
<h:form id="frmSingle">
<p:fileUpload id="upload" value="#{fileUploadView.file}"
mode="simple" skinSimple="true" auto="true"
process="@this" update="messages"
listener="#{fileUploadView.handleFileUpload}" />
</h:form>
</div>
<div class="card">
<h5>Single (allowTypes & sizeLimit CSV)</h5>
<h:form id="frmSingleCSV">
<p:fileUpload id="upload" value="#{fileUploadView.file}"
mode="simple" skinSimple="true" auto="true"
process="@this" update="messages"
listener="#{fileUploadView.handleFileUpload}">
<p:validateFile allowTypes="/(\.|\/)(pdf)$/" sizeLimit="100" />
</p:fileUpload>
</h:form>
</div>
<div class="card">
<h5>Multiple</h5>
<h:form id="frmMulti">
<p:fileUpload id="upload" value="#{fileUploadView.files}"
mode="simple" skinSimple="true" multiple="true" auto="true"
process="@this" update="messages"
listener="#{fileUploadView.handleFilesUpload}"/>
</h:form>
</div>
<div class="card">
<h5>Multiple (allowTyped & sizeLimit & fileLimit CSV)</h5>
<h:form id="frmMultiCSV">
<p:fileUpload id="upload" value="#{fileUploadView.files}"
mode="simple" skinSimple="true" multiple="true" auto="true"
process="@this" update="messages"
listener="#{fileUploadView.handleFilesUpload}"
allowTypes="/(\.|\/)(pdf)$/" sizeLimit="100" fileLimit="2" />
</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;
}
}