0

I just want to start a download of a pdf-file on a button-click. But in the way I'm doing that, the browser always opens the file.

<button id="target">Download</button>

$(document).on('click', '#target', function () {
    window.location.href = 'http://website.com/anything.pdf';
});
user3142695
  • 15,844
  • 47
  • 176
  • 332
  • [See here](http://stackoverflow.com/questions/2598658/how-to-force-a-pdf-download-automatically). It might require you to modify some [server side code](http://stackoverflow.com/questions/10576861/using-php-to-force-download-a-pdf) and send appropriate headers for downloadable content. – Lix Dec 03 '14 at 11:14
  • That means I can't do that directly? So I have to do that i.e. via a php-file which sends the correct header and opens the pdf, right? – user3142695 Dec 03 '14 at 11:22
  • Sounds like it... yes. This is a feature of the browser - it's trying to do you a favor by saying: "hey! I know this format - I can show it to you (that's probably what you want anyways...)". The way around this is to categorically say to the browser "Download this file!". – Lix Dec 03 '14 at 11:24

0 Answers0