2

I want to allow user to open or save PDF file rather than PDF file downloads it automatically.

Let us say i have a simple anchor tag

<a href="pdf/pdf-file1.pdf" > View PDF </a>

I tried to use download as well as type="application/pdf" in the anchor it self but it does not work.

I can force PDF file to open using C# code behind file, but I want to avoid that is there a simple HTML based way of doing it, not even in HTML 5

Learning
  • 19,469
  • 39
  • 180
  • 373
  • Unless you have some very, very, very good reasons, one should leave the way PDFs are handled and displayed to the user. If it is important to show the document in the browser, you can force this by rendering it server-side. – Max Wyss May 15 '16 at 10:05
  • I don't mind default behavior but it is a client request. Just wanted to know if it can be done using HTML only. I can do it from code behind using HTTP Handler also. – Learning May 15 '16 at 10:06

1 Answers1

0

Are you hosting this pdf yourself? If so, try adding target="_blank" to the anchor tag:

<a href="pdf/pdf-file1.pdf" target="_blank"> View PDF </a>

Otherwise, try an iframe:

<iframe src="pdf/pdf-file1.pdf" 
style="width:600px; height:800px;" frameborder="0"></iframe>

Also, if you're sending this file from your own backend set the Content-Disposition to inline in your response:

Content-Type: application/pdf
Content-Disposition: inline; filename="pdf-file1.pdf"
Anthony E
  • 11,072
  • 2
  • 24
  • 44
  • Yes this pdf file is on same server I had tried `target="_blank"` earlier it didnt work either – Learning May 15 '16 at 08:40
  • for last part of your solution i have to either add it in C# code or made changes to IIS setting which i dont want to. I want something simple to make it work if possible directly from the anchor tag – Learning May 15 '16 at 08:44
  • It's not possible to do with 100% reliability from the anchor tag because the browser determines how downloaded files will display. For best results, you should really change the `content-disposition` server-side. – Anthony E May 15 '16 at 08:45