Logo Blog

Allow user to download a file

Download via anchor tag

To provide a download link to the user you can use a normal anchor tag and include the download attribute.

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

Rename the downloaded file

You can pass a string to the download attribute to rename the file that is being downloaded.

<a href="/link/to/rick-astley.mp4" download="cats.mp4">Download file</a>

Renaming the file only works for files on the same domain as the website. You can find more information here.

Tell the browser how the file should be opened

You can use the Content-Disposition header to indicate whether the file should be displayed in the browser Content-Disposition: inline or downloaded Content-Disposition: attachment.

You can also set the filename via this header:

Content-Disposition: attachment; filename="my-image.jpg"