9

i am trying to make a download link in html that is given like this for a PDF Book

<a href="http://www.mydomain.org/pdf/book.pdf" target="_blank"> Download </a>

but problem is that when download link is clicked it opens online version of pdf , does not offer download , i did google and found same way to add download link , any one can guide me with it please whats wrong here

6 Answers6

14

As of late 2018, clicking the link won’t trigger a download if the resource to be downloaded wasn’t served from the same origin or same server. Apparently, this is restriction is a security measure.

You can download the content in browser and make it downloadable, you can check the below url

https://medium.com/charisol-community/downloading-resources-in-html5-a-download-may-not-work-as-expected-bf63546e2baa

madhu131313
  • 7,003
  • 7
  • 40
  • 53
  • I am opening an html page which is in my computer to test donwload links. it doesnt work. html page and file to be downloaded are both in my pc. – yigitt Jan 17 '22 at 07:03
5

you can try this

<a href="/pdf/book.pdf" download="book.pdf">Download </a>
  • i had already tried this but but thank u for this , accepted this as it was the right ans and anyone comes later will be helpful for him –  Nov 30 '13 at 13:32
  • 8
    This is correct. But "download" attribute is only works for resources with in same-origin. right? – Ishara Madawa Feb 26 '19 at 06:01
1

HTML5 defines the download attribute, which forces the browser to prompt the user a download dialog for the resource instead of navigate to it.

Here is the support across the different browsers: http://caniuse.com/#feat=download.

Carlo Cannas
  • 3,206
  • 19
  • 22
1

Its not the problem with your script, instead its your browser that has pdf plug-in and displays you the content file directly.You can just save the page (Press CTRL+S) and it would be saved as .PDF file.

Thanks.

Anirban
  • 550
  • 3
  • 19
0

The HTML5 download attribute is only supported by Chrome and firefox... Try this :

<a href="download.php" target="_blank"> Download </a>

Download.php

header("Content-disposition: attachment; filename=http://www.mydomain.org/pdf/book.pdf");
header("Content-type: application/pdf:");
readfile("http://www.mydomain.org/pdf/book.pdf");
Pain67
  • 326
  • 1
  • 9
-1

if you want to download the pdf in next tab with current website intact then use following code:

 <a href="/pdf/book.pdf" download="book.pdf" target="_blank">Download </a>
sticky bit
  • 36,626
  • 12
  • 31
  • 42