WebMasterCampus
WEB DEVELOPER Resources

Javascript Download File

Javascript Download File


Javascript Download File

Code Example

    <a download="File-1" href="file1.pdf">Download File</a>

    <ul>
        <li onclick="downloadFile('file1.pdf','File-1')">Download File</li>
    </ul>

    <script>
        function downloadFile (URL, fileName){
            let link = document.createElement("a");

            link.setAttribute("download", fileName);
            link.href = URL;

            document.body.appendChild(link);
            link.click();
            link.remove();
        }
    </script>
Created with love and passion.