FileUpload is an advanced uploader with drag and drop support, multi file uploads, auto uploading, progress tracking and validations.
import { FileUploadModule } from 'primeng/fileupload';
FileUpload basic mode provides a simpler UI as an alternative to default advanced mode.
<p-fileUpload
mode="basic"
chooseLabel="Choose"
chooseIcon="pi pi-upload"
name="demo[]"
url="https://www.primefaces.org/cdn/api/upload.php"
accept="image/*"
maxFileSize="1000000"
(onUpload)="onUpload($event)" />
When auto property is enabled, a file gets uploaded instantly after selection.
<p-fileUpload
mode="basic"
name="demo[]"
chooseIcon="pi pi-upload"
url="https://www.primefaces.org/cdn/api/upload.php"
accept="image/*" maxFileSize="1000000"
(onUpload)="onBasicUploadAuto($event)"
[auto]="true"
chooseLabel="Browse" />
Uploader UI is customizable using a ng-template called file that gets the File instance as the implicit variable. Second ng-template named content can be used to place custom content inside the content section which would be useful to implement a user interface to manage the uploaded files such as removing them. This template gets the selected files as the implicit variable. Third and final ng-template option is toolbar to display custom content at toolbar.
Drag and drop files here to upload.
<p-toast />
<p-fileUpload name="myfile[]" url="https://www.primefaces.org/cdn/api/upload.php" [multiple]="true" accept="image/*" maxFileSize="1000000" (onUpload)="onTemplatedUpload()" (onSelect)="onSelectedFiles($event)">
<ng-template pTemplate="header" let-files let-chooseCallback="chooseCallback" let-clearCallback="clearCallback" let-uploadCallback="uploadCallback">
<div class="flex flex-wrap justify-content-between align-items-center flex-1 gap-2">
<div class="flex gap-2">
<p-button (onClick)="choose($event, chooseCallback)" icon="pi pi-images" [rounded]="true" [outlined]="true" />
<p-button (onClick)="uploadEvent(uploadCallback)" icon="pi pi-cloud-upload" [rounded]="true" [outlined]="true" severity="success" [disabled]="!files || files.length === 0" />
<p-button (onClick)="clearCallback()" icon="pi pi-times" [rounded]="true" [outlined]="true" severity="danger" [disabled]="!files || files.length === 0" />
</div>
<p-progressBar [value]="totalSizePercent" [showValue]="false" styleClass="md:w-20rem h-1rem w-full md:ml-auto" [ngClass]="{ 'exceeded-progress-bar': totalSizePercent > 100 }">
<span class="white-space-nowrap">{{ totalSize }}B / 1Mb</span>
</p-progressBar>
</div>
</ng-template>
<ng-template pTemplate="content" let-files let-uploadedFiles="uploadedFiles" let-removeFileCallback="removeFileCallback" let-removeUploadedFileCallback="removeUploadedFileCallback">
<div *ngIf="files?.length > 0">
<h5>Pending</h5>
<div class="flex flex-wrap p-0 sm:p-5 gap-5">
<div *ngFor="let file of files; let i = index" class="card m-0 px-6 flex flex-column border-1 surface-border align-items-center gap-3">
<div>
<img role="presentation" [alt]="file.name" [src]="file.objectURL" width="100" height="50" />
</div>
<span class="font-semibold">{{ file.name }}</span>
<div>{{ formatSize(file.size) }}</div>
<p-badge value="Pending" severity="warning" />
<p-button icon="pi pi-times" (onClick)="onRemoveTemplatingFile($event, file, removeFileCallback, index)" [outlined]="true" [rounded]="true" severity="danger" />
</div>
</div>
</div>
<div *ngIf="uploadedFiles?.length > 0">
<h5>Completed</h5>
<div class="flex flex-wrap p-0 sm:p-5 gap-5">
<div *ngFor="let file of uploadedFiles; let i = index" class="card m-0 px-6 flex flex-column border-1 surface-border align-items-center gap-3">
<div>
<img role="presentation" [alt]="file.name" [src]="file.objectURL" width="100" height="50" />
</div>
<span class="font-semibold">{{ file.name }}</span>
<div>{{ formatSize(file.size) }}</div>
<p-badge value="Completed" class="mt-3" severity="success" />
<p-button icon="pi pi-times" (onClick)="removeUploadedFileCallback(index)" [outlined]="true" [rounded]="true" severity="danger" />
</div>
</div>
</div>
</ng-template>
<ng-template pTemplate="file"> </ng-template>
<ng-template pTemplate="empty">
<div class="flex align-items-center justify-content-center flex-column">
<i class="pi pi-cloud-upload border-2 border-circle p-5 text-8xl text-400 border-400"></i>
<p class="mt-4 mb-0">Drag and drop files here to upload.</p>
</div>
</ng-template>
</p-fileUpload>
FileUpload is an advanced uploader with dragdrop support, multi file uploads, auto uploading, progress tracking and validations.
<p-fileUpload
name="demo[]"
url="https://www.primefaces.org/cdn/api/upload.php"
(onUpload)="onUpload($event)"
[multiple]="true"
accept="image/*"
maxFileSize="1000000">
<ng-template pTemplate="content">
<ul *ngIf="uploadedFiles.length">
<li *ngFor="let file of uploadedFiles">
{{ file.name }} - {{ file.size }} bytes
</li>
</ul>
</ng-template>
</p-fileUpload>
Following is the list of structural style classes, for theming classes visit theming page.
Name | Element |
---|---|
p-fileupload | Container element |
p-fileupload-buttonbar | Header containing the buttons |
p-fileupload-content | Content section |
FileUpload uses a hidden native input element with type="file" for screen readers.
Interactive elements of the uploader are buttons, visit the Button accessibility section for more information.
API defines helper props, events and others for the PrimeNG FileUpload module.
FileUpload is an advanced uploader with dragdrop support, multi file uploads, auto uploading, progress tracking and validations.
Defines the input properties of the component.
Defines emit that determine the behavior of the component based on a given condition or report the actions that the component takes.
name | parameters | description | |
---|---|---|---|
onBeforeUpload | event : FileBeforeUploadEvent | Callback to invoke before file upload is initialized. | |
onSend | event : FileSendEvent | An event indicating that the request was sent to the server. Useful when a request may be retried multiple times, to distinguish between retries on the final event stream. | |
onUpload | event : FileUploadEvent | Callback to invoke when file upload is complete. | |
onError | event : FileUploadErrorEvent | Callback to invoke if file upload fails. | |
onClear | event : Event | Callback to invoke when files in queue are removed without uploading using clear all button. | |
onRemove | event : FileRemoveEvent | Callback to invoke when a file is removed without uploading using clear button of a file. | |
onSelect | event : FileSelectEvent | Callback to invoke when files are selected. | |
onProgress | event : FileProgressEvent | Callback to invoke when files are being uploaded. | |
uploadHandler | event : FileUploadHandlerEvent | Callback to invoke in custom upload mode to upload the files manually. | |
onImageError | event : Event | This event is triggered if an error occurs while loading an image file. | |
onRemoveUploadedFile | event : RemoveUploadedFileEvent | This event is triggered if an error occurs while removing an uploaded file. |
Defines methods that can be accessed by the component's reference.
Defines the templates used by the component.
Defines the custom events used by the component's emitters.
Upload event.
Remove uploaded file event.
Form data event.
An event indicating that the request was sent to the server. Useful when a request may be retried multiple times, to distinguish between retries on the final event stream.
Callback to invoke before file upload is initialized.
Callback to invoke when file upload is complete.
Callback to invoke when a file is removed without uploading using clear button of a file.
Callback to invoke when files are selected.
Callback to invoke when files are being uploaded.
Callback to invoke in custom upload mode to upload the files manually.
Callback to invoke on upload error.