File Selector

lightning-input type="file"

For Use In

Lightning Experience, Experience Builder Sites, Salesforce Mobile App, Lightning Out (Beta), Standalone Lightning App, Mobile Offline

An input field for selecting files to upload using an Upload Files button or a drag-and-drop zone. This field accepts files up to 3.5 MB.

To retrieve the list of selected files, use event.target.files in the onchange event handler.

lightning-input implements designs in the Salesforce Lightning Design System (SLDS). The input types adapt to SLDS 1 or SLDS 2 styling based on the org's theme or the container app that you use.

Input TypeSLDS 1SLDS 2
fileFile SelectorFile Selector

Selected files are returned in a FileList object, each specified as a File object with the size and type attributes.

  • lastModified
  • lastModifiedDate
  • name
  • size
  • type

For more information on the File object, see MDN web docs: Getting information on selected files.

To handle file uploads in your org, wire up your component to an Apex controller that creates a ContentVersion and ContentVersionLink object.

Alternatively, use the lightning-file-upload component for an integrated way to upload files to records.

To programmatically add a file, assign the file input to a FileList object, a File object, or an array of File objects. For example, you can add a new file to the files that are already attached.

To remove all files from the file list, use one of these methods.

To validate file selection, use the required attribute to ensure at least one file is selected. Use the accept attribute to restrict file types, and validate file size in the onchange event handler.

For the default error messages, see the lightning-input documentation.

To validate file selection, check the file list, file types, and file sizes. If the selected file isn't valid, the reportValidity() method shows the error message below the file selector.

The validity attribute returns an object with read-only boolean properties. For the file type, these attributes apply:

  • badInput - Indicates that the value is invalid for any input type
  • customError - Indicates that a custom error has been set using setCustomValidity()
  • valueMissing - Indicates that no file is selected when the required attribute is set
  • valid - True if none of the preceding properties are true

The component links the label attribute you provide with a unique ID on the file selector using the aria-labelledby attribute. The for attribute is also used to link the "Or drop files" label in the drop zone to the file selector.

If you use the label-hidden variant, the label attribute maintains the unique ID on the file selector using the aria-labelledby attribute. The component also maintains the for attribute to link between the file selector and the drop zone label.

If you use the field-level-help attribute, the component creates an aria-describedby link between the file selector and the help tooltip.

If the file selector fails validation, the component adds aria-invalid="true" and links the error message to the input by using aria-describedby.

For additional ARIA attributes that you can use, see the lightning-input Specifications tab.

Use these keys for keyboard navigation on the file selector.

  • Tab key: Users can tab to the "Choose Files" button.
  • Enter or Space key: Opens the standard OS file selector dialog

After the file selector dialog closes, focus returns to the component, preventing you from losing your place on the page.

change

The event fired when a value is changed in the input field.

The change event returns one of the following event.target parameters, depending on input type.

ParameterTypeDescription
filesObjectFor input type file, the list of selected files returned in a FileList object, each specified as a File object with the size and type attributes.
valuestringFor other input types, returns the input value.

The event properties are as follows.

PropertyValueDescription
bubblestrueThis event bubbles up through the DOM.
cancelablefalseThis event has no default behavior that can be canceled. You can't call preventDefault() on this event.
composedtrueThis event propagates outside of the component in which it was dispatched.

lightning-input

For this component's attributes and methods, see the lightning-input Specifications tab.