File Input

File inputs allow users to select files from their device.

File inputs allow users to select one or more files from their device using a dropzone that supports both click and drag-and-drop interactions.

This component works with standard <form> elements. Please refer to the section on form controls to learn more about form submission and client-side validation.

Examples

Link to This Section

Labels

Link to This Section

Use the label attribute to give the file input an accessible label. For labels that contain HTML, use the label slot instead.

Hints

Link to This Section

Add descriptive help text with the hint attribute. For hints that contain HTML, use the hint slot instead.

Multiple Files

Link to This Section

Add the multiple attribute to allow the file input to accept more than one file. If the user drops a folder, all files within it will be added to the file input.

Accepting File Types

Link to This Section

Use the accept attribute to limit the file input to certain file types. Set it to a comma-separated string of unique file type specifiers.

You can also use file extensions such as accept="pdf, .doc, .docx".

Capturing from a Camera

Link to This Section

On mobile devices, use the capture attribute to capture media directly from the device's camera or microphone instead of selecting an existing file. Set it to user for the front-facing camera or environment for the rear-facing one.

The capture attribute is only used when accept includes an image, video, or audio type. On desktop browsers and devices without the corresponding hardware, it is ignored and the file input behaves normally.

Disabled

Link to This Section

Use the disabled attribute to disable the file input.

Sizes

Link to This Section

Use the size attribute to change the file input's size.





Custom Dropzone Content

Link to This Section

Use the dropzone slot to customize what appears inside the dropzone area.

Drag and drop your files here or click to browse

Working with Files

Link to This Section

The files property gives you access to an array of selected files. Unlike the native file input's FileList, this is a standard JavaScript array, making it easier to manipulate.


Reverse Order Clear All Log Files

The files property must be reassigned, not mutated! Avoid using functions that mutate the array, such as reverse() and sort(), because they won't trigger an update. Use non-mutating alternatives like toReversed() and toSorted() instead.

Uploading with Forms

Link to This Section

When uploading files from a form, add method="post" and enctype="multipart/form-data" to the form so files are sent correctly to the server.


Upload

Required Validation

Link to This Section

Add the required attribute to make file selection mandatory. Form submission will be blocked until a file is selected.


Submit Reset

Custom Validation

Link to This Section

Use the setCustomValidity() method to set a custom error message. This will override standard validation and prevent form submission. Clear the error by passing an empty string.


Submit Reset

Styling Validation States

Link to This Section

Use the :state(user-valid) and :state(user-invalid) custom states to style the file input based on its validation status. These states only apply after the user has interacted with the control or attempted to submit the form.


Submit Reset

You can also style based on the :state(blank) and :state(dragging) states:

Importing

Link to This Section

If you're using the autoloader or a hosted project, components load on demand — no manual import needed. To cherry-pick a component manually, use one of the following snippets.

CDN npm Self-Hosted React

Import this component directly from the CDN:

import 'https://ka-f.webawesome.com/[email protected]/components/file-input/file-input.js';

After installing Web Awesome via npm, import this component:

import '@awesome.me/webawesome/dist/components/file-input/file-input.js';

If you're self-hosting Web Awesome, import this component from your server:

import './webawesome/dist/components/file-input/file-input.js';

To import this component for React 18 or below, use the following code:

import WaFileInput from '@awesome.me/webawesome/dist/react/file-input/index.js';

Slots

Link to This Section

Learn more about using slots.

Attributes & Properties

Link to This Section

Learn more about attributes and properties.

Methods

Link to This Section

Learn more about methods.

Events

Link to This Section

Learn more about events.

Custom States

Link to This Section

Learn more about custom states.

CSS parts

Link to This Section

Learn more about CSS parts.

Dependencies

Link to This Section

This component automatically imports the following elements. Sub-dependencies, if any exist, will also be included in this list.

Need a hand? Report a bug Ask for help