Advanced FileUpload is an advanced uploader with dragdrop support, multi file uploads, auto uploading, progress tracking and validations.
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.
Upload process begins once the file is selected in auto mode.
Chunked mode uploads files in (small) chunks to allow resume-functionality.
Per default, the FileUpload itself is the drop zone. In addition FileUpload supports a custom dropZone.
The empty-facet is rendered when no files are added yet.
Tooltips can be attached to each one of FileUpload buttons in advanced mode using PFS. Moreover, you can use plain html browser native titles as well.
<style>
body .ui-inputfield.ui-state-drag {
background: #ffffd6;
}
</style>
<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" listener="#{fileUploadView.handleFileUpload}"
mode="advanced" multiple="false"
accept=".gif,.jpg,.jpeg,.png"
update="growl"
onupload="return confirm('Are you sure?')">
<p:validateFile sizeLimit="1000000" fileLimit="3" allowTypes="/(\.|\/)(gif|jpeg|jpg|png)$/"
contentType="true"/>
</p:fileUpload>
<p:growl id="growl" showDetail="true" keepAlive="true"/>
</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" listener="#{fileUploadView.handleFileUpload}"
mode="advanced" multiple="true"
accept=".gif,.jpg,.jpeg,.png"
update="growl">
<p:validateFile sizeLimit="1000000" fileLimit="3" allowTypes="/(\.|\/)(gif|jpeg|jpg|png)$/"/>
</p:fileUpload>
<p:growl id="growl" showDetail="true" keepAlive="true"/>
</h:form>
</div>
<div class="card">
<h5>Auto</h5>
<p>Upload process begins once the file is selected in auto mode.</p>
<h:form id="auto" enctype="multipart/form-data">
<p:fileUpload id="upload" listener="#{fileUploadView.handleFileUpload}" mode="advanced"
update="growl" auto="true">
<p:validateFile sizeLimit="100000" allowTypes="/(\.|\/)(gif|jpeg|jpg|png)$/"/>
</p:fileUpload>
<p:growl id="growl" showDetail="true" keepAlive="true"/>
</h:form>
</div>
<div class="card">
<h5>Chunked</h5>
<p>Chunked mode uploads files in (small) chunks to allow resume-functionality.</p>
<h:form id="chunked" enctype="multipart/form-data">
<p:fileUpload id="upload" listener="#{fileUploadView.handleFileUpload}" mode="advanced"
multiple="true"
update="growl"
maxChunkSize="1000000">
<p:validateFile sizeLimit="10000000" fileLimit="3" allowTypes="/(\.|\/)(gif|jpeg|jpg|png)$/"/>
</p:fileUpload>
<p:growl id="growl" showDetail="true" keepAlive="true"/>
</h:form>
</div>
<div class="card">
<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 id="dropZone" enctype="multipart/form-data">
<div class="ui-fluid">
<div class="field">
<p:inputTextarea id="customDropZone" widgetVar="textarea"
rows="5" value="#{fileUploadView.dropZoneText}"/>
<small>
Drop on the text area to upload, or
<p:link value="choose" onclick="PF('uploadDnd').show();return false"
style="font-size:inherit"/>.
</small>
</div>
</div>
<p:fileUpload id="upload" widgetVar="uploadDnd" listener="#{fileUploadView.handleFileUploadTextarea}"
mode="advanced" auto="true"
dropZone="customDropZone"
update="@form"
style="display: none">
<p:validateFile allowTypes="/(\.|\/)(gif|jpeg|jpg|png)$/" sizeLimit="100000"/>
</p:fileUpload>
<p:growl id="growl" showDetail="true" keepAlive="true"/>
</h:form>
</div>
<div class="card">
<h5>Empty Facet</h5>
<p>The empty-facet is rendered when no files are added yet.</p>
<h:form id="empty" enctype="multipart/form-data">
<p:fileUpload id="upload" listener="#{fileUploadView.handleFileUpload}" mode="advanced"
multiple="true"
update="growl">
<f:facet name="empty">
Drag and drop files to here to upload.
</f:facet>
<p:validateFile sizeLimit="100000" fileLimit="3" allowTypes="/(\.|\/)(gif|jpeg|jpg|png)$/"/>
</p:fileUpload>
<p:growl id="growl" showDetail="true" keepAlive="true"/>
</h:form>
</div>
<div class="card">
<h5>Tooltips</h5>
<p>Tooltips can be attached to each one of FileUpload buttons in advanced mode using PFS. Moreover, you can use plain html browser native titles as well.</p>
<h:form id="tooltips" enctype="multipart/form-data">
<p:fileUpload id="uploader1" mode="advanced" styleClass="block mb-5"/>
<p:outputPanel id="tooltipsPanel">
<p:tooltip id="uploaderChooseFileBeforeUploadToolTip"
widgetVar="chooseWV" position="top"
for="@(#tooltips\:uploader1 > .ui-fileupload-buttonbar > .ui-fileupload-choose)"
value="Choose button tooltip"/>
<p:tooltip id="uploaderUploadButtonToolTip"
position="right"
for="@(#tooltips\:uploader1 > .ui-fileupload-buttonbar > .ui-fileupload-upload)"
value="Upload button tooltip" showEffect="clip"
hideEffect="explode"/>
<p:tooltip id="uploaderCancelButtonToolTip"
position="bottom"
for="@(#tooltips\:uploader1 > .ui-fileupload-buttonbar > .ui-fileupload-cancel)"
value="Cancel button tooltip"/>
</p:outputPanel>
<p:fileUpload id="uploader2" mode="advanced" styleClass="block mb-5"
chooseButtonTitle="Choose button tooltip"
uploadButtonTitle="Upload button tooltip"
cancelButtonTitle="Cancel button tooltip"/>
</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;
}
}