February 8th, 2007 by Luis Alves

Pdf opening but not in browser window

I needed this today and I found it, so now I’m posting it in my blog so there is another source to it.

I’m not the author of this code andI don’t know who is it… but it works, unless you have some anti-virus that prevents activex from working in your browser.

So, this asp code opens a pdf file but instead of opening it in a browser window, it downloads the document and opens it using your acrobat reader.

You should place this code before the html tag in your webpage. Good luck!

<%
Response.ContentType = "application/octet-stream"
FPath = server.mappath("../docs/Portugal.pdf")
Response.AddHeader "Content-Disposition","attachment; filename=" & FPath
Set adoStream = CreateObject("ADODB.Stream")
adoStream.Open()
adoStream.Type = 1
adoStream.LoadFromFile(FPath)
Response.BinaryWrite adoStream.Read()
adoStream.Close
Set adoStream = Nothing
Response.End
%>