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

Upload - 图1

Documentation

Import

  1. import {FileUploadModule} from 'primeng/fileupload';
  2.  

Getting Started

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

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

Multiple Uploads

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

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

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.

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

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.

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

File Size

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

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

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.

  1. <p-fileUpload name="myfile[]" url="./upload.php" multiple="multiple"
  2. accept="image/*" maxFileSize="1000000">
  3. <ng-template pTemplate="toolbar">
  4. <div>Upload 3 Files</div>
  5. </ng-template>
  6. <ng-template let-file pTemplate="file">
  7. <div>Custom UI to display a file</div>
  8. </ng-template>
  9. <ng-template pTemplate="content">
  10. <div>Custom UI to manage uploaded files</div>
  11. </ng-template>
  12. </p-fileUpload>
  13.  

Request Customization

Internally, FileUpload uses Angular's HttpClient module so you may take advantage of the built-in features such interceptors and header customization.

Basic UI

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

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

Custom Upload

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

  1. <p-fileUpload name="myfile[]" customUpload="true" (uploadHandler)="myUploader($event)"></p-fileUpload>
  2.  
  1. myUploader(event) {
  2. //event.files == files to upload
  3. }
  4.  

Properties

NameTypeDefaultDescription
namestringnullName of the request parameter to identify the files at backend.
urlstringnullRemote url to upload the files.
methodstringPOSTHTTP method to send the files to the url.
multiplebooleanfalseUsed to select multiple files at once from file dialog.
acceptstringfalsePattern to restrict the allowed file types such as "image/*".
disabledbooleanfalseDisables the upload functionality.
autobooleanfalseWhen enabled, upload begins automatically after selection is completed.
maxFileSizenumbernullMaximum file size allowed in bytes.
invalidFileSizeMessageSummarystring"{0}: Invalid file size, "Summary message of the invalid fize size.
invalidFileSizeMessageDetailstring"maximum upload size is {0}."Detail message of the invalid fize size.
invalidFileTypeMessageSummarystring"{0}: Invalid file type, "Summary message of the invalid fize type.
invalidFileTypeMessageDetailstring"allowed file types: {0}."Detail message of the invalid fize type.
stylestringnullInline style of the component.
styleClassstringnullStyle class of the component.
previewWidthnumber50Width of the image thumbnail in pixels.
chooseLabelstringChooseLabel of the choose button.
uploadLabelstringUploadLabel of the upload button.
cancelLabelstringCancelLabel of the cancel button.
withCredentialsbooleanfalseCross-site Access-Control requests should be made using credentials such as cookies, authorization headers or TLS client certificates.
modestringadvancedDefines the UI of the component, possible values are "advanced" and "basic".
customUploadbooleanfalseWhether to use the default upload or a manual implementation defined in uploadHandler callback.
showUploadButtonbooleantrueDefines the visibility of upload button for Client-side FileUpload.
showCancelButtonbooleantrueDefines the visibility of cancel button for Client-side FileUpload.
filesarraynullList of files to be provide to the FileUpload externally.

Events

NameParametersDescription
onBeforeUploadevent.formData: FormData object.Callback to invoke before file upload begins to customize the request such as post parameters before the files.
onBeforeSendevent.formData: FormData object.Callback to invoke before file send begins to customize the request such as adding headers.
onUploadevent.files: Uploaded files. event.originalEvent: Http EventCallback to invoke when file upload is complete.
onErrorevent.files: Files that are not uploaded. event.error: Request Error.Callback to invoke if file upload fails.
onClear-.Callback to invoke when files in queue are removed without uploading using clear all button.
onRemoveevent.originalEvent: Original browser event. event.file: Selected file.Callback to invoke when a file is removed without uploading using clear button of a file.
onSelectevent.originalEvent: Original browser event. event.files: List of selected files.Callback to invoke when files are selected.
onProgressevent.originalEvent: Original browser event. event.progress: Calculated progress value.Callback to invoke when files are being uploaded.
uploadHandlerevent.files: List of selected files.Callback to invoke in custom upload mode to upload the files manually.

Methods

NameParametersDescription
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.

NameElement
ui-fileuploadContainer element
ui-fileupload-buttonbarHeader containing the buttons
ui-fileupload-contentContent section

Dependencies

None.

Source

View on GitHub

  1. <p-toast [style]="{marginTop: '80px'}"></p-toast>
  2. <h3 class="first">Advanced</h3>
  3. <p-fileUpload name="demo[]" url="./upload.php" (onUpload)="onUpload($event)"
  4. multiple="multiple" accept="image/*" maxFileSize="1000000">
  5. <ng-template pTemplate="content">
  6. <ul *ngIf="uploadedFiles.length">
  7. <li *ngFor="let file of uploadedFiles">{{file.name}} - {{file.size}} bytes</li>
  8. </ul>
  9. </ng-template>
  10. </p-fileUpload>
  11. <h3>Basic</h3>
  12. <p-fileUpload mode="basic" name="demo[]" url="./upload.php" accept="image/*" maxFileSize="1000000" (onUpload)="onBasicUpload($event)"></p-fileUpload>
  13. <h3>Basic with Auto</h3>
  14. <p-fileUpload #fubauto mode="basic" name="demo[]" url="./upload.php" accept="image/*" maxFileSize="1000000" (onUpload)="onBasicUploadAuto($event)" auto="true" chooseLabel="Browse"></p-fileUpload>
  15.  
  1. export class FileUploadDemo {
  2. uploadedFiles: any[] = [];
  3. constructor(private messageService: MessageService) {}
  4. onUpload(event) {
  5. for(let file of event.files) {
  6. this.uploadedFiles.push(file);
  7. }
  8. this.messageService.add({severity: 'info', summary: 'File Uploaded', detail: ''});
  9. }
  10. }
  11.