Download a File Using JavaScript

To download a file using JavaScript you can use the browser's built-in functionality rather than writing your own function.

Step 1

The first step is to create an HTML anchor element with the href attribute set to the URL of the file to download.


<a href="/link/to/file.mp4">Download</a>


Step 2

Now add a download attribute. This changes the behaviour of the link to open a download dialogue when the link is clicked.


<a href="/link/to/file.mp4" download>Download</a>


Step 3

If you want the file to have a specific download name, set it inside the download attribute like this:


<a href="/link/to/file.mp4" download="filename.mp4">Download</a>