# How do you implement file upload functionality in LWC using lightning-file-upload?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, File Upload, Base Components

Read it on the site: https://www.forcechallenges.com/interview-questions/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.

**Code / config example:**
```javascript
import { LightningElement, api } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';

export default class Uploader extends LightningElement {
    @api recordId;
    get accepted() { return ['.pdf', '.png', '.jpg', '.docx']; }

    handleFinished(event) {
        const files = event.detail.files; // [{ name, documentId }]
        this.dispatchEvent(new ShowToastEvent({
            title: 'Uploaded',
            message: files.length + ' file(s) uploaded',
            variant: 'success'
        }));
    }
}
// html: <lightning-file-upload label="Attach" record-id={recordId}
//        accept={accepted} multiple onuploadfinished={handleFinished}></lightning-file-upload>
```

**Hidden gotchas & must-remember points:**
- It uploads immediately on selection; there is no "save" step to cancel.
- `accept` filters in the picker but is not a security control; validate server-side if it matters.
- Triggers on ContentVersion fire per file; bulk-safe code is needed.
- The file is linked to a record only if `record-id` is provided.
- Guest-user and Lightning Out support is restricted; verify per release.
- Custom base64 uploads are bound by Apex heap and request limits; avoid for large files.

**Best practice:** Use `lightning-file-upload` by default, and only build a custom uploader when you need special processing before the file lands in Salesforce.

---
Source: ForceChallenges · last updated 2026-11-02
