FileUpload FileUpload is an advanced uploader with dragdrop support, multi file uploads, auto uploading, progress tracking and validations.

Advanced

  • {{file.name}} - {{file.size}} bytes

Basic

Basic with Auto

Import


import {FileUploadModule} from 'primeng/fileupload';

Getting Started

FileUpload requires a url property as the upload target and a name to identify the files at backend.


<p-fileUpload name="myfile[]" url="./upload.php"></p-fileUpload>

Multiple Uploads

Only one file can be selected at a time, to allow selecting multiples enable multiple option.


<p-fileUpload name="myfile[]" url="./upload.php" multiple="multiple"></p-fileUpload>

DragDrop

File selection can also be done by dragging and dropping one or more to the content section of the component.

Auto Uploads

When auto property is enabled, upload begins as soon as file selection is completed or a file is dropped on the drop area.


<p-fileUpload name="myfile[]" url="./upload.php" multiple="multiple"
        accept="image/*" auto="auto"></p-fileUpload>

File Types

Selectable file types can be restricted with accept property, example below only allows images to be uploaded. Read more about other possible values here.


<p-fileUpload name="myfile[]" url="./upload.php" multiple="multiple"
        accept="image/*"></p-fileUpload>

File Size

Maximium file size can be restricted using maxFileSize property defined in bytes.


<p-fileUpload name="myfile[]" url="./upload.php" multiple="multiple"
        accept="image/*" maxFileSize="1000000"></p-fileUpload>

In order to customize the default messages use invalidFileSizeMessageSummary and invalidFileSizeMessageDetail options. In summary {0} placeholder refers to the filename and in detail, file size.

  • invalidFileSizeMessageSummary: '{0}: Invalid file size, '
  • invalidFileSizeMessageDetail: string = 'maximum upload size is {0}.'

Templating

File list 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. Third and final ng-template option is "toolbar" to display custom content at toolbar.


<p-fileUpload name="myfile[]" url="./upload.php" multiple="multiple"
        accept="image/*" maxFileSize="1000000">
        <ng-template pTemplate="toolbar">
            <div>Upload 3 Files</div>
        </ng-template>  
        <ng-template let-file pTemplate="file">
            <div>Custom UI to display a file</div>
        </ng-template> 
        <ng-template pTemplate="content">
            <div>Custom UI to manage uploaded files</div>
        </ng-template>  
</p-fileUpload>

Request Customization

XHR request to upload the files can be customized using the onBeforeUpload callback that passes the xhr instance and FormData object as event parameters.

Basic UI

FileUpload basic mode provides a simpler UI as an alternative to advanced mode.


<p-fileUpload mode="basic" name="demo[]" url="./upload.php" accept="image/*" maxFileSize="1000000" (onUpload)="onBasicUpload($event)" auto="true"></p-fileUpload>

Custom Upload

Uploading implementation can be overriden by enabling customMode property and defining a custom upload handler event.


<p-fileUpload name="myfile[]" customUpload="true" (uploadHandler)="myUploader($event)"></p-fileUpload>


myUploader(event) {
    //event.files == files to upload
}

Properties

Name Type Default Description
name string null Name of the request parameter to identify the files at backend.
url string null Remote url to upload the files.
method string POST HTTP method to send the files to the url.
multiple boolean false Used to select multiple files at once from file dialog.
accept string false Pattern to restrict the allowed file types such as "image/*".
disabled boolean false Disables the upload functionality.
auto boolean false When enabled, upload begins automatically after selection is completed.
maxFileSize number null Maximum file size allowed in bytes.
invalidFileSizeMessageSummary string "{0}: Invalid file size, " Summary message of the invalid fize size.
invalidFileSizeMessageDetail string "maximum upload size is {0}." Detail message of the invalid fize size.
invalidFileTypeMessageSummary string "{0}: Invalid file type, " Summary message of the invalid fize type.
invalidFileTypeMessageDetail string "allowed file types: {0}." Detail message of the invalid fize type.
style string null Inline style of the component.
styleClass string null Style class of the component.
previewWidth number 50 Width of the image thumbnail in pixels.
chooseLabel string Choose Label of the choose button.
uploadLabel string Upload Label of the upload button.
cancelLabel string Cancel Label of the cancel button.
withCredentials boolean false Cross-site Access-Control requests should be made using credentials such as cookies, authorization headers or TLS client certificates.
mode string advanced Defines the UI of the component, possible values are "advanced" and "basic".
customUpload boolean false Whether to use the default upload or a manual implementation defined in uploadHandler callback.
showUploadButton boolean true Defines the visibility of upload button for Client-side FileUpload.
showCancelButton boolean true Defines the visibility of cancel button for Client-side FileUpload.
files array null List of files to be provide to the FileUpload externally.

Events

Name Parameters Description
onBeforeUpload event.xhr: XmlHttpRequest instance.
event.formData: FormData object.
Callback to invoke before file upload begins to customize the request such as post parameters before the files.
onBeforeSend event.xhr: XmlHttpRequest instance.
event.formData: FormData object.
Callback to invoke before file send begins to customize the request such as adding headers.
onUpload event.xhr: XmlHttpRequest instance.
event.files: Uploaded files.
Callback to invoke when file upload is complete.
onError event.xhr: XmlHttpRequest instance.
event.files: Files that are not uploaded.
Callback to invoke if file upload fails.
onClear -. Callback to invoke when files in queue are removed without uploading using clear all button.
onRemove event.originalEvent: Original browser event.
event.file: Selected file.
Callback to invoke when a file is removed without uploading using clear button of a file.
onSelect event.originalEvent: Original browser event.
event.files: List of selected files.
Callback to invoke when files are selected.
onProgress event.originalEvent: Original browser event.
event.progress: Calculated progress value.
Callback to invoke when files are selected.
uploadHandler event.files: List of selected files. Callback to invoke in custom upload mode to upload the files manually.

Methods

Name Parameters Description
upload - Uploads the selected files.
clear - Clears the files list.

Styling

Following is the list of structural style classes, for theming classes visit theming page.

Name Element
ui-fileupload Container element
ui-fileupload-buttonbar Header containing the buttons
ui-fileupload-content Content section

Dependencies

None.

View on GitHub

<p-growl [value]="msgs"></p-growl>
    
<h3 class="first">Advanced</h3>
<p-fileUpload name="demo[]" url="./upload.php" (onUpload)="onUpload($event)" 
        multiple="multiple" 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>

<h3>Basic</h3>
<p-fileUpload mode="basic" name="demo[]" url="./upload.php" accept="image/*" maxFileSize="1000000" (onUpload)="onBasicUpload($event)"></p-fileUpload>

<h3>Basic with Auto</h3>
<p-fileUpload #fubauto mode="basic" name="demo[]" url="./upload.php" accept="image/*" maxFileSize="1000000" (onUpload)="onBasicUploadAuto($event)" auto="true" chooseLabel="Browse"></p-fileUpload>


export class FileUploadDemo {

    msgs: Message[];
    
    uploadedFiles: any[] = [];

    onUpload(event) {
        for(let file of event.files) {
            this.uploadedFiles.push(file);
        }
    
        this.msgs = [];
        this.msgs.push({severity: 'info', summary: 'File Uploaded', detail: ''});
    }
}