I'm receiving a link to a file once the component is loaded, and i want to download it.
html
<template>
<lightning-card>
<div class="slds-align_absolute-center" style="height:5rem">
<lightning-button variant="brand" label="Download" title="Download" icon-position="right" icon-name="utility:download" onclick = {downloadClick} class="slds-m-left_x-small"></lightning-button>
</div>
</lightning-card>
</template>
When i press the Download
button, i want to download the file in thr url i received.
I tried creating an a
tag in js, and clicking it. it redirects me to the link instead of downloading it:
downloadClick() {
let downloadElement = document.createElement('a');
downloadElement.href = 'https://www.w3schools.com/images/myw3schoolsimage.jpg';
downloadElement.target = '_self';
downloadElement.download = 'download.jpg';
document.body.appendChild(downloadElement);
downloadElement.click();
}
What am i doing wrong?