Problem Statement
What does the download attribute in an anchor tag do?
Explanation
The download attribute prompts the browser to download the linked file instead of navigating to it. This is useful when linking to files like PDFs, images, documents, or zip files that you want users to save rather than view in the browser. You can use the download attribute in two ways. First, just add download without a value, and the file will be downloaded with its original name. Second, provide a value like download equals new filename dot pdf to suggest a different filename for the downloaded file. The download attribute only works for same-origin URLs or blob and data URLs. For security reasons, it does not work for cross-origin links. This attribute improves user experience by making it clear that clicking the link will download a file.
Code Solution
SolutionRead Only
<!-- Download with original filename --> <a href="document.pdf" download>Download PDF</a> <!-- Download with custom filename --> <a href="report.pdf" download="Annual-Report-2024.pdf">Download Report</a> <!-- Download image --> <a href="photo.jpg" download="my-photo.jpg">Download Image</a> <!-- Must be same-origin for security --> <a href="/files/data.zip" download>Download ZIP</a>
