We help IT Professionals succeed at work.

Open PDF file within IE browser (in a more secure way)

waiman asked
I try to open a PDF file within the IE browser.  I know you can just open a PDF file inside without problem but you get all the address stuff within the browser which may not be desirable.  I have got a routine within my ASP like this:
sub WriteFile(urlpath)
     dim objStream
     set objStream = server.CreateObject("ADODB.Stream")
     Response.ContentType = "application/pdf"
    Response.BinaryWrite objStream.Open "URL=" &  urlpath, adModeRead
     Set objStream = Nothing
end sub

However when I tried that I can only view the Acrobat splash screen but then nothing within the browser.  I am wondering if I did something wrong.  I have tried calling files within my PC (without the 'URL=' bit, of course) but the same thing happens.

Thanks for your help in advance.
Watch Question

Rounded IT Consultant / Projects manager / Cloud Consultant / IT Planning
You are opening the pdf incorrectly..
do it like this

'Make your check for logged users
If Session("Logged") = "" then response.redirect "login.asp"
send needed headers
strFileName = fileNameorpath
Response.ContentType = "application/pdf"
'Response.AddHeader "Content-disposition", "attachment; filename=" & strFileName & ".pdf"
'un-comment the above line if you want to force the users to save the file, otherwise it will open in the browser if acrobat reader plugin for the browser is installed
' contents here...

set objBinaryFile = Server.CreateObject("BinaryFileStream.Object")
Response.BinaryWrite objBinaryFile.GetFileBytes(CStr(strFileName))
set objBinaryFile = nothing


The "attachment" attribute is a directive to the browser to save, not to show, the document. It doesn't
work with IE 4.01 as this browser has a bug with it (it's a RFC though). If you want the browser to
open the document by default, use "inline" instead.


BinaryFileStream object dll can be found here:

Response.AddHeader "content-length", objFile.Size
enables the browser to show a progress bar to the user... "downloading xyz.pdf. 3.4MB from 5.8MB"

also if the pdf is remote use xmldom object:

How do I read the contents of a remote web page?    
You can include static txt and HTML files from remote servers by using a component (such as AspHTTP (http://www.serverobjects.com/products.htm), ASPTear (http://www.alphasierrapapa.com/ComponentCenter/AspTear/), or VB's built in InetCtrls) to parse the remote URL's content.
You can also try this method out, it uses the MSXML objects which are installed with Windows 2000 (or you can download them here (http://download.microsoft.com/download/xml/Install/3.0/WIN98Me/EN-US/msxml3.exe) for other operating systems):
    url = "http://www.espn.com/
    set xmlhttp = server.CreateObject("Microsoft.XMLHTTP")
    xmlhttp.open "GET", url, false
    xmlhttp.send "" 
    Response.write xmlhttp.responseText

Explore More ContentExplore courses, solutions, and other research materials related to this topic.