473,416 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Working With Files Above Site's Root Directory

I'm having just a bit of trouble wrapping my brain around the task of
working with folders that are above the site's root folder.

I let users upload photos (.jpg/.gif files) which can subsequently be viewed
on the site's pages. My hosting provider is requiring that any files my Web
app writes get written to a folder that is above the app's root folder (for
security purposes).

When writing the files I understand how to use MapPath to get the physical
path to the destination folder. So I think I'm okay on the uploading part.

But what I'm kind of hung up on is how to get my the aspx pages (<IMG
src=...>) to refer to the files in that folder. I'm dynamically assigning
the value of the src= property of IMG controls.

Do I need to create a virtual folder in the site? Is that the solution? Is
there another/better way? If I create a virtual folder, can I then ditch the
use of MapPath for uploading the files? Suppose I get all this to work with
a virtual folder, then haven't we totally defeated the whole point of
placing the folder above the site root (for security purposes)?

Bottom line - what do I need to do?

I'm kind of new Web app development, so any guidance or perspective is
greatly appreciated.
Nov 19 '05 #1
4 7546
Hi Jerry,

You wont be able to use Server.MapPath, as the file is outside of your web
application. It is more useful to think of the file location as "outside"
rather than "above" in this case, as the physical location of the directory
isn't important. The fact that it is outside your web app is important.

There are 2 kinds of directories in an ASP.Net app. One type is virtual
directories, or web directories. These are objects in the web server, as a
result of them having been defined as inside a web. They correspond to file
system directories, but are completely different in most respects. File
System directories can't have web server permissions assigned to them. File
System directories are invisible to the web server, unless they are mapped
to virtual directories.

The most typical way that a virtual directory is created is "by default,"
that is, as a result of being under the root web directory. They can also be
created by mapping directories outside the web app to a virtual directory
INSIDE a web app. This illustrates one of the differences between virtual
(web) directories and file system directories. Virtual directory permissions
pertain to HTTP requests for content. File System directory permissions
pertain to file system access of the directory.

So, in order to upload files, you're going to have to hard-code the full
(file system) directory location to upload them to. Your server-side code
can save the files to the physical file system directory that way.

To fetch them, you will also have to use the file system location. Rather
than creating an image tag that has a "src" attribute (which can only point
to a URL), you need to create an ASP page or HTTP Handler to fetch the
images from the file system, and return them, from inside your web. That ASP
page would be the "src" attribute of the image tag, and you can add
QueryString parameters to indicate which file it should fetch. It would set
the Response.ContentType property to "image/jpg" (or whatever MIME type the
image is), and save the file after opening it to the Response.OutputStream.
IOW, it would read the QueryString, find the file system location of the
image file, open it, and save it to the Response.OutputStream.

An HTTPHandler would also do the trick, but is probably a bit more than you
need for this requirement.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.

"Jerry" <no****@nospam.org> wrote in message
news:uq**************@TK2MSFTNGP09.phx.gbl...
I'm having just a bit of trouble wrapping my brain around the task of
working with folders that are above the site's root folder.

I let users upload photos (.jpg/.gif files) which can subsequently be viewed on the site's pages. My hosting provider is requiring that any files my Web app writes get written to a folder that is above the app's root folder (for security purposes).

When writing the files I understand how to use MapPath to get the physical
path to the destination folder. So I think I'm okay on the uploading part.

But what I'm kind of hung up on is how to get my the aspx pages (<IMG
src=...>) to refer to the files in that folder. I'm dynamically assigning
the value of the src= property of IMG controls.

Do I need to create a virtual folder in the site? Is that the solution? Is
there another/better way? If I create a virtual folder, can I then ditch the use of MapPath for uploading the files? Suppose I get all this to work with a virtual folder, then haven't we totally defeated the whole point of
placing the folder above the site root (for security purposes)?

Bottom line - what do I need to do?

I'm kind of new Web app development, so any guidance or perspective is
greatly appreciated.

Nov 19 '05 #2
Thanks Kevin - you always give great explanations.
"Kevin Spencer" <ks******@takempis.com> wrote in message
news:uN**************@tk2msftngp13.phx.gbl...
Hi Jerry,

You wont be able to use Server.MapPath, as the file is outside of your web
application. It is more useful to think of the file location as "outside"
rather than "above" in this case, as the physical location of the directory isn't important. The fact that it is outside your web app is important.

There are 2 kinds of directories in an ASP.Net app. One type is virtual
directories, or web directories. These are objects in the web server, as a
result of them having been defined as inside a web. They correspond to file system directories, but are completely different in most respects. File
System directories can't have web server permissions assigned to them. File System directories are invisible to the web server, unless they are mapped
to virtual directories.

The most typical way that a virtual directory is created is "by default,"
that is, as a result of being under the root web directory. They can also be created by mapping directories outside the web app to a virtual directory
INSIDE a web app. This illustrates one of the differences between virtual
(web) directories and file system directories. Virtual directory permissions pertain to HTTP requests for content. File System directory permissions
pertain to file system access of the directory.

So, in order to upload files, you're going to have to hard-code the full
(file system) directory location to upload them to. Your server-side code
can save the files to the physical file system directory that way.

To fetch them, you will also have to use the file system location. Rather
than creating an image tag that has a "src" attribute (which can only point to a URL), you need to create an ASP page or HTTP Handler to fetch the
images from the file system, and return them, from inside your web. That ASP page would be the "src" attribute of the image tag, and you can add
QueryString parameters to indicate which file it should fetch. It would set the Response.ContentType property to "image/jpg" (or whatever MIME type the image is), and save the file after opening it to the Response.OutputStream. IOW, it would read the QueryString, find the file system location of the
image file, open it, and save it to the Response.OutputStream.

An HTTPHandler would also do the trick, but is probably a bit more than you need for this requirement.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Neither a follower
nor a lender be.

"Jerry" <no****@nospam.org> wrote in message
news:uq**************@TK2MSFTNGP09.phx.gbl...
I'm having just a bit of trouble wrapping my brain around the task of
working with folders that are above the site's root folder.

I let users upload photos (.jpg/.gif files) which can subsequently be

viewed
on the site's pages. My hosting provider is requiring that any files my

Web
app writes get written to a folder that is above the app's root folder

(for
security purposes).

When writing the files I understand how to use MapPath to get the physical path to the destination folder. So I think I'm okay on the uploading part.
But what I'm kind of hung up on is how to get my the aspx pages (<IMG
src=...>) to refer to the files in that folder. I'm dynamically assigning the value of the src= property of IMG controls.

Do I need to create a virtual folder in the site? Is that the solution? Is there another/better way? If I create a virtual folder, can I then ditch

the
use of MapPath for uploading the files? Suppose I get all this to work

with
a virtual folder, then haven't we totally defeated the whole point of
placing the folder above the site root (for security purposes)?

Bottom line - what do I need to do?

I'm kind of new Web app development, so any guidance or perspective is
greatly appreciated.


Nov 19 '05 #3
And Merry Christmas to you, Jerry!

--
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.

"Jerry" <no****@nospam.org> wrote in message
news:Om**************@TK2MSFTNGP09.phx.gbl...
Thanks Kevin - you always give great explanations.
"Kevin Spencer" <ks******@takempis.com> wrote in message
news:uN**************@tk2msftngp13.phx.gbl...
Hi Jerry,

You wont be able to use Server.MapPath, as the file is outside of your web
application. It is more useful to think of the file location as "outside" rather than "above" in this case, as the physical location of the directory
isn't important. The fact that it is outside your web app is important.

There are 2 kinds of directories in an ASP.Net app. One type is virtual
directories, or web directories. These are objects in the web server, as a result of them having been defined as inside a web. They correspond to

file
system directories, but are completely different in most respects. File
System directories can't have web server permissions assigned to them.

File
System directories are invisible to the web server, unless they are mapped to virtual directories.

The most typical way that a virtual directory is created is "by default," that is, as a result of being under the root web directory. They can also be
created by mapping directories outside the web app to a virtual
directory INSIDE a web app. This illustrates one of the differences between virtual (web) directories and file system directories. Virtual directory

permissions
pertain to HTTP requests for content. File System directory permissions
pertain to file system access of the directory.

So, in order to upload files, you're going to have to hard-code the full
(file system) directory location to upload them to. Your server-side code can save the files to the physical file system directory that way.

To fetch them, you will also have to use the file system location. Rather than creating an image tag that has a "src" attribute (which can only

point
to a URL), you need to create an ASP page or HTTP Handler to fetch the
images from the file system, and return them, from inside your web. That

ASP
page would be the "src" attribute of the image tag, and you can add
QueryString parameters to indicate which file it should fetch. It would

set
the Response.ContentType property to "image/jpg" (or whatever MIME type

the
image is), and save the file after opening it to the

Response.OutputStream.
IOW, it would read the QueryString, find the file system location of the
image file, open it, and save it to the Response.OutputStream.

An HTTPHandler would also do the trick, but is probably a bit more than

you
need for this requirement.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Neither a follower
nor a lender be.

"Jerry" <no****@nospam.org> wrote in message
news:uq**************@TK2MSFTNGP09.phx.gbl...
I'm having just a bit of trouble wrapping my brain around the task of
working with folders that are above the site's root folder.

I let users upload photos (.jpg/.gif files) which can subsequently be

viewed
on the site's pages. My hosting provider is requiring that any files
my Web
app writes get written to a folder that is above the app's root folder

(for
security purposes).

When writing the files I understand how to use MapPath to get the physical path to the destination folder. So I think I'm okay on the uploading part.
But what I'm kind of hung up on is how to get my the aspx pages (<IMG
src=...>) to refer to the files in that folder. I'm dynamically assigning the value of the src= property of IMG controls.

Do I need to create a virtual folder in the site? Is that the
solution? Is there another/better way? If I create a virtual folder, can I then

ditch the
use of MapPath for uploading the files? Suppose I get all this to work

with
a virtual folder, then haven't we totally defeated the whole point of
placing the folder above the site root (for security purposes)?

Bottom line - what do I need to do?

I'm kind of new Web app development, so any guidance or perspective is
greatly appreciated.



Nov 19 '05 #4
Kevin Spencer wrote:
Hi Jerry,

You wont be able to use Server.MapPath, as the file is outside of your web
application. It is more useful to think of the file location as "outside"
rather than "above" in this case, as the physical location of the
directory isn't important. The fact that it is outside your web app is
important.

There are 2 kinds of directories in an ASP.Net app. One type is virtual
directories, or web directories. These are objects in the web server, as a
result of them having been defined as inside a web. They correspond to
file system directories, but are completely different in most respects.
File System directories can't have web server permissions assigned to
them. File System directories are invisible to the web server, unless they
are mapped to virtual directories.

The most typical way that a virtual directory is created is "by default,"
that is, as a result of being under the root web directory. They can also
be created by mapping directories outside the web app to a virtual
directory INSIDE a web app. This illustrates one of the differences
between virtual (web) directories and file system directories. Virtual
directory permissions pertain to HTTP requests for content. File System
directory permissions pertain to file system access of the directory.

So, in order to upload files, you're going to have to hard-code the full
(file system) directory location to upload them to. Your server-side code
can save the files to the physical file system directory that way.

To fetch them, you will also have to use the file system location. Rather
than creating an image tag that has a "src" attribute (which can only
point to a URL), you need to create an ASP page or HTTP Handler to fetch
the images from the file system, and return them, from inside your web.
That ASP page would be the "src" attribute of the image tag, and you can
add QueryString parameters to indicate which file it should fetch. It
would set the Response.ContentType property to "image/jpg" (or whatever
MIME type the image is), and save the file after opening it to the
Response.OutputStream. IOW, it would read the QueryString, find the file
system location of the image file, open it, and save it to the
Response.OutputStream.

An HTTPHandler would also do the trick, but is probably a bit more than
you need for this requirement.

Beautiful explanation!
Do you write?
Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Anand K Rayudu | last post by:
Hi all, I am trying to find a way to get the files recursively in a given directory, The following code is failing, can some one please suggest what could be problem here from os import...
6
by: Peter Maas | last post by:
My goal is to have the top level of a directory tree in the Python path without touching anything outside the directory. I tried to create .pth files with the top level path in every subdirectory...
29
by: Patrick | last post by:
I have the following code, which regardless which works fine and logs to the EventViewer regardless of whether <processModel/> section of machine.config is set to username="SYSTEM" or "machine" ...
7
by: Bennett Haselton | last post by:
If you create a ASP.Net Web application in Visual Studio .Net, you apparently have to specify the target as a directory on a web server, e.g. www.hostname.com/dirname, with "dirname" being the name...
9
by: jab3 | last post by:
So I'm considering a small project that involves online file storage. Let's say I wanted to set up a site that allows people to log-on, create an account, and then have space to upload files. The...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
4
by: tshad | last post by:
I have a site www.stf.com and a site www.stfstage.com (where I do all my testing). The problem is that www.stfstage.com is only internal and I need to get access from the outside (without...
18
by: BDE Consulting | last post by:
I am going crazy. This has been a problem now for over a year and I have yet to figure out what is causing it. I have a single server that is running multiple domains. For this example I will...
6
by: josequinonesii | last post by:
I've searched, I've read, I've tested and re-read numerous post but to no avail yet... Quite simply, the settings I've applied to my httpd.conf, httpd-vhost.conf and my hosts files simply does not...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.