Explain Codes LogoExplain Codes Logo

How can I create download links in HTML?

html
responsive-design
best-practices
download-links
Anton ShumikhinbyAnton Shumikhin·Jan 15, 2025
TLDR

A download link in HTML is created using the <a> tag in combination with the download attribute:

<a href="yourfile.zip" download>Download the file</a>

Replace "yourfile.zip" with the path and name of your actual file. Clicking this link activates a direct download of the file.

Extending compatibility across browsers

Though the download attribute is robust, its support isn't universal across all browsers, particularly older ones. To ensure your download links work on all platforms, use server-side techniques, such as the Content-Disposition header.

For servers running Apache, the following can be added to your .htaccess or apache2.conf:

AddType application/octet-stream .zip

Replace .zip with the file extension in question. This line forces the server to send headers that urge the browser to download the linked file.

Customizing the download file name

To provide a specific name for the downloaded file, which may be different from the source filename, set a value for the download attribute:

<a href="path/to/file" download="UniqueFilename.zip">Download the unique file</a>

This technique allows users to better organize their downloads by providing them with specific names for their downloaded files.

Ensuring secure downloads and improving user experience

Ensure your server set up and scripts handle file serving securely. Another way of enhancing the user experience is by preventing navigation away from your page during the download. This can be achieved by adding target="_blank":

<a href="file.zip" download target="_blank">Download in new window</a>

Always be clear to your users by giving your download links descriptive names, so they know exactly what they are downloading.

Using server-side scripts for on-the-fly downloads

If your site needs to provide downloads of dynamic content or files created on-the-fly, you'll likely leverage a server-side scripting language like PHP, Python, or Node.js. These server-side scripts can also set headers to ensure downloads are treated correctly. Here's an example in PHP:

<?php // Header to force download header('Content-Disposition: attachment; filename="newdownload.pdf"'); // Read file content from a path or generate dynamically readfile('path/to/original.pdf'); ?>

Replace newdownload.pdf with the filename that the users will get, and path/to/original.pdf with the location of the actual file.

Advanced techniques for user- and file-friendly downloads

Downloading non-standard files

For less common or custom file types, set the Content-Type and Content-Disposition headers on the server to avoid misinterpretations.

Accessibility enhancements

Ensure accessibility by adding appropriate aria-label attributes to your download links:

<a href="file.zip" download aria-label="Download ZIP file">Download File</a>

Streamlining multiple downloads

If your site offers multiple files for download, consider compressing them into a single downloadable archive (for example, a ZIP file).