To allow file upload to a public library through Lightning Web Component (LWC) and generate a public link, you need to:
- Create an LWC to handle file upload.
- Implement Apex methods to upload the file to Salesforce and generate a public link.
fileUploader.html
<template>
<lightning-card title="File Uploader" icon-name="custom:custom63">
<lightning-input type="file" label="Select File" onchange={handleFileChange}></lightning-input>
<lightning-button label="Upload File" onclick={handleFileUpload} class="slds-m-top_medium"></lightning-button>
<template if:true={publicLink}>
<lightning-input label="Public Link" value={publicLink} readonly></lightning-input>
</template>
</lightning-card>
</template>
fileUploader.js
import { LightningElement, track } from 'lwc';
import uploadFileAndGenerateLink from '@salesforce/apex/FileUploaderWithPublicLink.uploadFileAndGenerateLink';
export default class FileUploader extends LightningElement {
@track publicLink;
fileData;
handleFileChange(event) {
const file = event.target.files[0];
if (file) {
this.fileData = {
fileName: file.name,
fileContent: ''
};
const reader = new FileReader();
reader.onload = () => {
const base64 = reader.result.split(',')[1];
this.fileData.fileContent = base64;
};
reader.readAsDataURL(file);
}
}
handleFileUpload() {
if (this.fileData) {
uploadFileAndGenerateLink({ fileName: this.fileData.fileName, fileContent: this.fileData.fileContent })
.then(result => {
this.publicLink = result;
})
.catch(error => {
console.error('Error uploading file: ', error);
});
}
}
}
FileUploaderWithPublicLink class
public class FileUploaderWithPublicLink {
@AuraEnabled
public static String uploadFileAndGenerateLink(String fileName, String fileContent) {
String libraryName = 'Asset Library'; //you can change library name
// Fetch the public library Id dynamically
Id libraryId = getLibraryIdByName(libraryName);
if (libraryId == null) {
throw new AuraHandledException('Library not found: ' + libraryName);
}
// Step 1: Upload the file
ContentVersion contentVersion = new ContentVersion();
contentVersion.Title = fileName;
contentVersion.PathOnClient = fileName;
contentVersion.VersionData = EncodingUtil.base64Decode(fileContent);
contentVersion.FirstPublishLocationId = libraryId; // Id of the public library
insert contentVersion;
// Query the ContentVersionId
//ContentVersion uploadedContentVersion = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id = :contentVersion.Id LIMIT 1];
// Step 2: Create ContentDistribution record to generate a public link
ContentDistribution contentDistribution = new ContentDistribution();
contentDistribution.Name = fileName;
contentDistribution.ContentVersionId = contentVersion.Id;
contentDistribution.PreferencesAllowOriginalDownload = true; // Set to false if you do not want to allow original download
contentDistribution.PreferencesAllowPDFDownload = true; // Set to false if you do not want to allow PDF download
contentDistribution.PreferencesAllowViewInBrowser = true; // Set to false if you do not want to allow view in browser
contentDistribution.PreferencesExpires = false; // Set to true if you want the link to expire
contentDistribution.PreferencesPasswordRequired = false; // Set to true if you want to require a password
insert contentDistribution;
// Query the secure URL
ContentDistribution createdContentDistribution = [SELECT Id, DistributionPublicUrl, ContentDownloadUrl FROM ContentDistribution WHERE Id = :contentDistribution.Id LIMIT 1];
// Return the public link URL
return createdContentDistribution.ContentDownloadUrl;
}
private static Id getLibraryIdByName(String libraryName) {
List<ContentWorkspace> libraries = [SELECT Id FROM ContentWorkspace WHERE Name = :libraryName LIMIT 1];
if (!libraries.isEmpty()) {
return libraries[0].Id;
}
return null;
}
}
Demo
Once you select the file and click on upload, file will be uploaded in ‘Asset Library’ library, public link will be generated which you will see at lwc UI as well at the bottom as well-

Now you can modify this component according to your need.