Develop Secure Code
generateUrl
generateUrlGenerates and returns a URL for downloading a file in an LWR site in Experience Cloud.
1import { generateUrl } from "lightning/fileDownload";
2const url = generateUrl(recordId);
3window.open(url);recordID—(Required) The record ID of the file (ContentDocument, ContentVersion, Attachment, or Document) to be downloaded.
A URL for downloading a file.
Authenticated users can download files they have access to. Guest users can download only ContentDocument files that they have access to through Library membership.
This code sample generates a URL for downloading a file.
1import { LightningElement, api } from "lwc";
2import { generateUrl } from "lightning/fileDownload";
3
4export default class Download extends LightningElement {
5 @api recordId;
6 url;
7
8 handleClick() {
9 this.url = generateUrl(this.recordId);
10 }
11}