How can we set it so that when we click on the download link in an HTML page, the download is done on the same page using JavaScript?

Its possible to we can download the file from current page by using javascript.

<script>
  function downloadFile(link) {
            const fileUrl = link;
            const a = document.createElement('a');
            a.href = fileUrl;
            a.download = 'filename.txt';
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
        }
</script>
<p class="product-img-link"><a class="product-img-link-hover" onclick="downloadFile('../SSP Applications/NetSuite Inc. - SCA 2023.1.0/Development/pdf/SMARTadvantageGI.zip')" target="_blank">SMART Advantage Glass Ionomer Restorative</a></p>

In <p>, we are using the onClick method to download the file and passing the URL as a parameter

Leave a comment

Your email address will not be published. Required fields are marked *