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.
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.
1handleAttachFiles(){2 // Create a reference to lightning-input lwc:ref="inputFile"3 const myFileField = this.refs.inputFile;45 // Create a File object6 const myFile = new File(['test content'], 'test.txt', {type: 'text/plain'});78 // Add the new file to existing files9 const currentFiles = myFileField.files ? Array.from(myFileField.files) : [];10 myFileField.files = [...currentFiles, myFile];11}
To remove all files from the file list, use one of these methods.
1// Method 1: Clear files using DataTransfer2myFileField.files = new DataTransfer().files;34// Method 2: Clear files using an empty array5myFileField.files = [];
Input Validation
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.
1<template>2 <lightning-input3 type="file"4 label="Upload Document"5 accept=".pdf,.doc,.docx"6 required7 multiple8 lwc:ref="documentUpload"9 onchange={handleFilesChange}10 message-when-value-missing="Please select at least one file"11 >12 </lightning-input>13 <lightning-button label="Submit" onclick={handleSubmit}></lightning-button>14</template>
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.
1import{LightningElement}from "lwc";23export default class FileValidation extends LightningElement{4 maxFileSize = 3500000; // 3.5 MB56 handleFilesChange(event){7 const files = event.target.files;8 if(files.length === 0){9 return;10}1112 const fileInput = this.refs.documentUpload;1314 // Validate file sizes15 for(let file of files){16 if(file.size>this.maxFileSize){17 // Display error message18 fileInput.setCustomValidity(`File ${file.name} exceeds the maximum size of 3.5 MB`);19 fileInput.reportValidity();20 return;21}22}2324 fileInput.setCustomValidity("");25 // Proceed with file upload26}2728 handleSubmit(){29 const fileInput = this.refs.documentUpload;30 console.log(fileInput.validity.valid); // Returns true or false31 if(!fileInput.checkValidity()){32 fileInput.reportValidity();33 return;34}35 // Proceed with form submission36}37}
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
Accessibility
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.