FileInput
FileInput selects files by drag and drop, paste or the file picker, validates each file and uploads it with its own progress. It has a list, an images and an avatar variant, and works with every kind of Angular form.
Import #
import { FileInput } from '@exhaustive-ui/core/fileinput';Basic #
FileInput is bound with ngModel, and its value is the array of selected files. Files are added by clicking the dropzone, with the keyboard, by drag and drop, or by pasting them, a screenshot for example, while the component has the focus. Dropping a folder adds the files it contains and keeps their relative path. Without an url or an uploadHandler, nothing is uploaded: the files stay in the value, ready to be sent with the form.
Upload #
Each file is uploaded in its own request, with its own progress bar, as soon as it is added. Set url to post the files with HttpClient, which requires provideHttpClient(), or pass an uploadHandler function to upload them yourself, straight to cloud storage for example. The handler receives the file, an AbortSignal and a progress callback, and returns a promise of the server response. At most concurrency files upload at the same time, 3 by default. An upload in progress can be canceled, and a failed or canceled upload can be retried. Set auto to false to start the uploads with the upload() method instead.
The demo below simulates the server. Files with "error" in their name fail.
Posting to an endpoint only needs its url. The form data field is named file, or the value of fieldName.
<p-file-input url="/api/upload" fieldName="document" [headers]="headers" withCredentials />Validation #
accept takes extensions such as .pdf and MIME types such as image/*. maxFileSize and minFileSize limit the size of each file, maxFiles the number of files, existing files included, and maxTotalSize the size of all the files together, in bytes. A rejected file stays in the list with the reason next to it until the user removes it, and it is not part of the value. Use hint to state the rules under the dropzone.
Images #
The images variant shows the files as a grid of square thumbnails, for a gallery, a listing or a product page. The first image is marked as the cover, the progress of an upload is drawn over its thumbnail, and the add tile, which also accepts dropped files, stays at the end of the grid until maxFiles is reached.
Avatar #
The avatar variant is a single clickable image to replace a profile picture or a logo, round by default or square with avatarShape. It holds one file, so its value is a file or null. The image shown comes from the chosen file, otherwise from the first existing file. On mobile devices, capture opens the camera directly.
Existing Files #
When a form edits a record, pass the files already stored on the server to existing. They are listed before the new files, with a download button when they have a url and a delete button. Deleting one emits onRemoveExisting: delete the file on the server from there. Existing files count toward maxFiles and are not part of the value.
Template #
The dropzone template replaces the content of the dropzone and receives an open function and an active flag, true while files are dragged over it. The item template replaces a row of the list and receives the item with its remove, retry and cancel functions.
Sizes #
FileInput provides small and large sizes as alternatives to the default, like the other form fields.
Labels #
Every text of the component, error messages and screen reader announcements included, can be replaced through labels. Missing keys keep their English default. Placeholders such as {0} receive the values. Sizes use the fileSizeTypes units of the locale.
Disabled #
When disabled is present, files can no longer be added or removed.
Invalid #
The invalid state is applied using the invalid property to indicate failed validation.
Forms #
Template Driven #
The value is an array of files, empty when nothing is selected, so required works as on the other fields. Resetting the form clears the files.
Reactive Forms #
FileInput can also be used with reactive forms. In this case, the formControlName property is used to bind the component to a form control.
Signal Forms #
FileInput can also be used with Angular Signal Forms. The [formField] directive binds the component to a field holding an array of files. Signal Forms' required does not treat an empty array as empty, so use minLength(field, 1) to require at least one file.
Accessibility #
Screen Reader
The dropzone, the add tile and the avatar are buttons that open the native file picker, which stays hidden. Describe them with ariaLabelledBy or ariaLabel; the hint is linked to the dropzone with aria-describedby. The file list is labelled, every action button names its file, for example "Remove report.pdf", and progress bars use the progressbar role. A polite live region announces the files added and rejected, finished and failed uploads, and removed files.
<span id="attachments">Attachments</span>
<p-file-input ariaLabelledBy="attachments" hint="PDF only, 5 MB maximum" />
<p-file-input variant="avatar" ariaLabel="Profile picture" />Keyboard Support
| Key | Function |
|---|---|
| tab | Moves focus to the dropzone, then to the action buttons of each file. |
| enter / space | Opens the file picker from the dropzone, the add tile or the avatar, or runs the focused action. |
| ctrl / cmd + v | Adds the files of the clipboard while the component has the focus. |
When a file is removed, the focus moves to the next action button, or back to the dropzone.
