Simple uploader uses native browser file upload.
Per default, simple FileUpload just renders the native HTML markup.
Single mode allows only one file to be selected at a time from the native file dialog.
Multiple mode allows multiple files to be selected from the native file dialog, if supported by the browser.
FileUpload integrates p:validateFile for validation. Even Client-Side-Validation is supported.
Auto-Mode uploads the file immediately via AJAX.
Use Case to demonstrate the integrity for multiple features.
Use Case to demonstrate the integrity for multiple features.
Per default, the FileUpload itself is the drop zone. In addition FileUpload supports a custom dropZone.
<style>
body .card.ui-state-drag {
background-color: #ffffd6;
}
</style>
<p:growl id="growl" showDetail="true" keepAlive="true"/>
<div class="card">
<h5>Unskinned</h5>
<p>Per default, simple FileUpload just renders the native HTML markup.</p>
<h:form id="unskinned" enctype="multipart/form-data">
<p:fileUpload id="upload" value="#{fileUploadView.file}" mode="simple"/>
<p:commandButton id="submit" value="Submit" ajax="false" action="#{fileUploadView.upload}"
styleClass="mt-3 ui-button-outlined block"/>
</h:form>
</div>
<div class="card">
<h5>Single</h5>
<p>Single mode allows only one file to be selected at a time from the native file dialog.</p>
<h:form id="single" enctype="multipart/form-data">
<p:fileUpload id="upload" value="#{fileUploadView.file}" mode="simple" skinSimple="true"/>
<p:commandButton id="submit" value="Submit" ajax="false" action="#{fileUploadView.upload}"
styleClass="mt-3 ui-button-outlined block"/>
</h:form>
</div>
<div class="card">
<h5>Multiple</h5>
<p>Multiple mode allows multiple files to be selected from the native file dialog, if supported by the browser.</p>
<h:form id="multiple" enctype="multipart/form-data">
<p:fileUpload id="upload" value="#{fileUploadView.files}" mode="simple" skinSimple="true"
multiple="true"/>
<p:commandButton id="submit" value="Submit" ajax="false" action="#{fileUploadView.uploadMultiple}"
styleClass="mt-3 ui-button-outlined block"/>
</h:form>
</div>
<div class="card">
<h5>Single & Validation</h5>
<p>FileUpload integrates p:validateFile for validation. Even Client-Side-Validation is supported.</p>
<h:form id="singleValidation" enctype="multipart/form-data">
<p:fileUpload id="upload" value="#{fileUploadView.file}" mode="simple" skinSimple="true">
<p:validateFile allowTypes="/(\.|\/)(pdf)$/"/>
</p:fileUpload>
<p:commandButton id="submit" value="Submit" action="#{fileUploadView.upload}"
styleClass="mt-3 ui-button-outlined block"
validateClient="false" process="@form" update=":growl"/>
<p:commandButton id="submitCSV" value="Submit (+ validateClient)" action="#{fileUploadView.upload}"
styleClass="mt-3 ui-button-outlined block"
validateClient="true" process="@form" update=":growl"/>
</h:form>
</div>
<div class="card">
<h5>Auto & Single</h5>
<p>Auto-Mode uploads the file immediately via AJAX.</p>
<h:form id="autoSingle">
<p:fileUpload id="upload" value="#{fileUploadView.file}"
mode="simple" skinSimple="true" auto="true"
process="@this" update=":growl"
listener="#{fileUploadView.handleFileUpload}"/>
</h:form>
</div>
<div class="card">
<h5>Auto & Single & allowTyes validation</h5>
<p>Use Case to demonstrate the integrity for multiple features.</p>
<h:form id="autoSingleValidation">
<p:fileUpload id="upload" value="#{fileUploadView.file}"
mode="simple" skinSimple="true" auto="true"
process="@this" update=":growl"
listener="#{fileUploadView.handleFileUpload}">
<p:validateFile allowTypes="/(\.|\/)(pdf)$/" sizeLimit="100"/>
</p:fileUpload>
</h:form>
</div>
<div class="card">
<h5>Auto & Multiple</h5>
<p>Use Case to demonstrate the integrity for multiple features.</p>
<h:form id="autoMultiple">
<p:fileUpload id="upload" value="#{fileUploadView.files}"
mode="simple" skinSimple="true" multiple="true" auto="true"
process="@this" update=":growl"
listener="#{fileUploadView.handleFilesUpload}"/>
</h:form>
</div>
<div class="card" jsf:id="dropzone">
<h5>Custom Drag&Drop</h5>
<p>Per default, the FileUpload itself is the drop zone. In addition FileUpload supports a custom dropZone.</p>
<h:form>
<p:growl id="growl" showDetail="true"/>
<p:fileUpload listener="#{fileUploadView.handleFileUpload}"
mode="simple"
dropZone="@form:@parent"
skinSimple="true"
auto="true"
update="@form">
<p:validateFile allowTypes="/(\.|\/)(gif|jpeg|jpg|png)$/" sizeLimit="100000"/>
</p:fileUpload>
</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;
}
}