Introduction
Uploading files to Salesforce is a common requirement, but ensuring they are appropriately distributed for public or internal access can be challenging. Salesforce Content Distribution allows us to generate public links for files stored in Salesforce Content.
This blog will guide you through the convenient process of automatically uploading files with a Lightning Web Component (LWC) and creating Content Distribution records. This process allows us to generate shareable links, which help render materials inside or outside Salesforce or include images when creating PDF documents.
Step 1: Uploading Files Using LWC
Salesforce provides the lightning-file-upload component, which allows users to upload files and associate them with a record.
LWC Component for File Upload
This component enables users to upload files and calls Apex to process them.
LWC HTML (fileUploadComponent.html)

LWC JavaScript (fileUploadComponent.js)

How This Works
- Users upload files using lightning-file-upload.
- The handleUploadFinished method retrieves the uploaded file’s documentId.
- The createContentDistribution Apex method generates a public link for the uploaded file.
Â
Step 2: Creating Content Distribution in Apex
Once a file is uploaded, we must generate a public link using Content Distribution.
Apex Controller (ContentDistributionHandler.cls)

Step 3: Verifying the Implementation
Once a file is uploaded:
- The lightning-file-upload component uploads it to Salesforce Files (ContentVersion).
- The Apex method creates a ContentDistribution record for the file.
The public link can be retrieved from the DistributionPublicUrl field.
Conclusion
This implementation provides a seamless experience for users to Upload files via LWC and access the ContentDistribution records for public sharing or downloading.
- By automating the creation of ContentDistribution records, this process saves time and effort. It also ensures that public URLs are generated for easy file-sharing, enhancing the user experience.
- Generate public URLs for easy file-sharing.
Key Benefits
- Automation: No manual sharing is required.
- With this process, access to public URLs can be controlled via preferences, ensuring the security of the shared files.
- Efficiency: LWC handles file uploads asynchronously, allowing you to focus on other tasks while the system works in the background.
This approach is perfect for applications requiring file sharing, document management, or public downloads.

Public URL being displayed after successful image upload
Authors: Gella Sangamesh Gupta