Introducing generateUrl for Lightning Web Components.
Easily generate and return a URL for downloading files in Experience Cloud.
With the generateUrl function, you can retrieve a URL for downloading files within your LWR site.
Syntax:
import { generateUrl } from "lightning/fileDownload";
const url = generateUrl(recordId);
window.open(url);
Parameters:
- recordID (Required):
The record ID of the file (ContentDocument, ContentVersion, Attachment, or Document) you want to download.
Returns:
A URL that allows you to download the specified file.
Usage:
- Authenticated users can download files they have proper access to.
- Guest users can download ContentDocument files only if they have access via Library membership.
Sample Code:
import { LightningElement } from "lwc";
import { generateUrl } from "lightning/fileDownload";
export default class Download extends LightningElement {
recordId;
url;
handleClick() {
this.url = generateUrl(this.recordId);
}
}
Follow Us