1

I would like to have an a tag (anchor) that triggers a download when clicked.

I've already tried all the solutions in this S/O question and more: How to trigger a file download when clicking an html button or javascript and non works, except for this one:

<a href="lorem.gif" download="lorem.gif">HTML 5 Download</a>

This one works in the latest versions of Firefox and Chrome for certain file types, like images, but doesn't work for PDFs and it doesn't work on IE and many other browsers either.

Community
  • 1
  • 1
vromancas
  • 183
  • 2
  • 11
  • @MaxZoom is a PDF or an image a malware? It is intended for commercial sites. Thanks for the opportunity to clarify it. – vromancas Apr 17 '15 at 14:10

2 Answers2

1

You are looking for the Content-Disposition HTTP header. You set it on the server-side when you are sending down the file:

Content-Disposition: attachment; filename=lorem.pdf;

It basically tells the browsers that they should download the file instead of trying to render it. See this SO question or this Wikipedia page: MIME#Content-Disposition.

Community
  • 1
  • 1
JosiahDaniels
  • 2,411
  • 20
  • 37
0

Hrere is how to download the file

<a href="lorem.gif" download="lorem.gif"><button>Download</button></a>
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – mech Feb 27 '16 at 01:06