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 Lightning Web Runtime (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);
}
}
In the provided sample code, the generateUrl function is imported from "lightning/fileDownload". Inside the Download Lightning Web Component, a method called handleClick is defined, which calls the generateUrl function with a recordId to retrieve the URL for the file. The URL is then assigned to the url property, which can be used to open the file using window.open(url) or perform other actions with it.
Happy coding, everyone!
Comments
Post a Comment
Please Write your comment here.