šŸ“™ Understanding Neuronal Networks presale is now open - 20% off discount!

The download attribute of the link tags in HTML

Cool stuff learned today! For any a anchor tag we can add the download attribute and that link becomes a download link instead of a navigational one.

<a href="/my-file.pdf" download >
    Download the file 
</a>

It works with any type of file. You can download images, pdfs, documents, you name it!

Hot to Create a Downloadable Link using the Download Attribute in HTML

Please note that the download attribute only works for same-origin files. You can just use files from your own domain to isolate potentially malicious threats.

Setting a name for the downloaded file when using the download attribute

If you just add the download attribute to an a tag then the file that is downloaded will have the same name as the file in the href attribute.

<a href="/my-file.pdf" download>
    Download with the same name
</a>
<!-- original file: my-file.pdf
     downloaded file:  my-file.pdf -->

But, we can set up a new name for the downloaded file by giving a value to the download attribute:

<a href="/my-file.pdf" download="manual.pdf">
    Download with a different name
</a>
<!-- original file: my-file.pdf
     downloaded file:  manual.pdf -->

Note that even if it's possible to set the extension in the file, it's much better not to add it. If you just type the new filename, without the extension, the original extension will be kept. This is especially useful when working with images.

<a href="/avatar.png" download="picture">
    Download with a different name, keep the file extension
</a>
<!-- original file: avatar.png
     downloaded file:  picture.png -->

šŸ“– Neural Networks from Scratch - Presale

I'm writing a book about the timeless foundational concepts of neural networks for JavaScript developers. Go from if-else to weights and biases by building tiny AI models from scratch!

šŸ“– Neural Networks from Scratch - Presale

I'm writing a book about the timeless foundational concepts of neural networks for JavaScript developers. Go from if-else to weights and biases by building tiny AI models from scratch!


Leave a Reply

Your email address will not be published. Required fields are marked *