How do you implement file upload functionality in LWC using lightning-file-upload?
Answer
Basics: Add <lightning-file-upload record-id={recordId} accept={acceptedFormats} multiple onuploadfinished={handleFinished}>. Users pick files; the component uploads them as ContentVersion records, creating a ContentDocument linked to the record by ContentDocumentLink. It includes a progress UI and file-type restrictions via accept (for example .pdf,.png).
How it works: The uploadfinished event provides event.detail.files, an array containing name and documentId (and content version info). Use those ids to show a list, refresh a related list or call Apex to post-process. If record-id is omitted, the files are uploaded but not linked to a record (you can link later). Optional file-field-name and file-field-value let you populate a custom field on ContentVersion at upload.
Advanced / deep dive: The file is uploaded by the platform rather than your JavaScript, so it avoids Apex heap and request-size limits that hurt custom base64 approaches (lightning-input type="file" plus FileReader and Apex is limited by the 6 MB sync heap and request limits). Sharing: the uploader becomes owner, the linked record controls visibility (ShareType V/I/C on the link), and standard file permissions and the "Files" feature licensing apply. Number and size limits are org-configurable and documented (default simultaneous files and per-file size cap; verify in current release notes). Not all contexts are supported, so check availability for Experience Cloud guest users and Lightning Out. For custom logic, trigger Apex on ContentDocumentLink (triggers on ContentDocument have limitations) or react in onuploadfinished. To rename or classify files, update ContentVersion after the event.