Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Open file from outside the web root folder

Posted on 2004-08-25
Medium Priority
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 = objFilestream.name
     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 ADODB.stream 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 100 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 100 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

618 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