0

On my website I have a downloads page and I want to know how to make a button that will cause the user to download a zip file.

  • 3
    Does this answer your question? [How to trigger a file download when clicking an HTML button or JavaScript](https://stackoverflow.com/questions/11620698/how-to-trigger-a-file-download-when-clicking-an-html-button-or-javascript) – SuperStormer Apr 30 '21 at 00:27

1 Answers1

0

If you really want to have button instead of link, you can it like this:

<button onclick="loadFile()">Download</button>

and JavaScript:

function loadFile() {
    var a = document.createElement("a");
    a.href = "./fileToDownload.zip";
    a.download = "true";
    document.body.append(a);
    a.click();
    document.body.removeChild(a);
}

If you consider using link, you can do it much easier:

<a href="./fileToDownload.zip" download >Download</a>
Pyry Lahtinen
  • 459
  • 1
  • 3
  • 6
  • @BlackHatGorilla if you found my answer helpful, please mark it as accepted. You can read more here: https://stackoverflow.com/help/accepted-answer – Pyry Lahtinen May 01 '21 at 05:22