Open file from outside the web root folder

Posted on 2004-08-25
Last Modified: 2008-02-01

We have website designed in ASP and MS SQL Server 2000. We have some confidential documents which were placed outside the web root folder. For security reasons we put the documents folder out side the web.

For ex: Our web folder location : D:\Inetpub\wwwroot\xxxwebsite
            Our documents location : D:\documents

All ASP Pages are in xxxwebsite folder. I want to open the file (i.e pdf/doc/xls/etc) from D:\Documents directory using ASP page

How do I do this?

Please help on this.

Question by:AKantareddy
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2

Expert Comment

ID: 11892287
if YOU can open your file through an application means any internet guest user can ... so that means security is as much as u have in wwwroot folder ....

you can have two solutions

1- keep you files in the database

2- copy the file you want to view in a temp folder in wwwroot ... view and after viewing ... delete the file from there

oh ... and a third solution ...

3- create a VB dll that access the PC as an admin or desktop user ... call the dll from your ASP page to open the file ... this way desktop will think that some desktop user is opening the file not the internet user.

LVL 46

Expert Comment

ID: 11892297
You will have to use streaming--here is an example:

Function downloadFile( strFile, strDownloadFilename )
     Dim strFilename,objStream,objFilesystem,objFilestream
     Dim intFileLength
     ' get full path of specified file
     strFilename = FullPath
     ' clear the buffer
     Response.Buffer = True

     ' create stream
     Set objStream = Server.CreateObject("ADODB.Stream")

     ' set as binary
     objStream.Type = 1

     ' check the file exists
     Set objFilesystem = Server.CreateObject("Scripting.FileSystemObject")
     if not objFilesystem.FileExists(strFilename) then
          Response.Write("<h1>Error</h1>: " & strFilename & " does not exist<p>")
     end if

     ' get length of file
     Set objFilestream = objFilesystem.GetFile( strFilename )
     intFilelength = objFilestream.size
     objStream.LoadFromFile( strFilename )
     if err then
          Response.Write("<h1>Error: </h1>" & err.Description & "<p>")
     end if
     'format strFileName
     if Len( Trim(strDownloadFilename) ) > 0 then
          strDownloadFilename = Trim( strDownloadFilename )
          strDownloadFilename =
     end if
'     Response.ContentType = "SENTREnet"

     ' send the headers to the users browser
     Response.AddHeader "Content-Disposition", "attachment; filename=" & strDownloadFilename
     Response.AddHeader "Content-Length", intFilelength
     Response.Charset = "UTF-8"
     dim i
      for i = 0 to objFilestream.size
            i = i + 128000

     ' output the file to the browser
     'Response.BinaryWrite objStream.Read

     ' tidy up
     Set objFilestream = Nothing
End Function
Call downloadFile( Replace( Request("FILE") ,"/","\"), Request("FILENAME") )
'Response.Write (Request("FILE")  & "----" & Request("FILENAME") )

Expert Comment

ID: 11893181
Streaming, as FtB suggested, is your best bet.  This will ensure your security structure remains intact.

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 11893405

I tried with method which FtB mentioned

But the files are reading from the physical path (i.e. D:\documents\test.pdf).

1) My web folder directory is D:\Inetpub\wwwroot\xxxwebsite. and I want to read the file from  D:\documents\test.pdf. I tried but it does not read.
strFilename  = " D:\documents\test.pdf"

2) I placed the test.pdf file in D:\Inetpub\wwwroot\xxxwebsite. and tried with physical path.
It didn't work.
strFilename  = " D:\Inetpub\wwwroot\xxxwebsite\test.pdf"

3) I tried with Server.Mappath("test.pdf") then it worked fine.
strFilename  = "Server.Mappath("test.pdf")

But I want to read the file only from physical path since it is out side the web folder.

LVL 46

Expert Comment

ID: 11893520
What do you get if you do:

response.write Server.Mappath("test.pdf")

LVL 46

Expert Comment

ID: 11893545
I am asking because server.MapPath() should return a physical path, and you should be able to use that here. Let's try within the web directory first. Once we get that working, we can remove to a remote directory. I have used this in a few different applications where we had the same security concerns.

LVL 15

Expert Comment

ID: 11894885
<<I tried but it does not read. strFilename  = " D:\documents\test.pdf">>
Do you get an error?  
Does iusr_machine have premission to access the file?

Accepted Solution

dwaldner earned 25 total points
ID: 11894942
Try this, as I have done the exact same thing, and it works fine:
    file = "D:\documents\test.pdf"  
    Dim objStream
    Set objStream = Server.CreateObject("ADODB.Stream")
    objStream.Type = 1
    Response.AddHeader "Content-Disposition", "attachment;filename=""test.pdf"""
    Response.ContentType = "APPLICATION/OCTET-STREAM"                                                                                                                              
    Response.BinaryWrite objStream.Read
    Set objStream = Nothing

Author Comment

ID: 11895687
Hi dwaldner,

Thanks for you response

I tried your code. It is not giving any error

But it displays the file download dialog box to open the file

I clicked on open button and it asked to selected from the program list

I selected open thru Adobe PDF then it opened the file

How can we open without displaying the dialog box

LVL 15

Assisted Solution

joeposter649 earned 25 total points
ID: 11895790
Get rid of the response.addheader and change the content type to Response.ContentType="application/pdf"

Author Comment

ID: 11897119


I tried opening PDF, DOC, XLS, TXT files. They are working fine

Thank you for all


Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question