Uploading files to a virtual directory

B

B Letts

Hi -

I'm currently using the FileUpload control to allow people to upload
files to my website. This all works fine, as long as I'm going to a
physical path on my server.

However, I need to allow people to upload to a virtual directory. The
directory may or may not exist on the same server as the Web site.

I can't seem to figure out how to do this. If I pass the virtual
directory address to FileUpload.SaveAs, it tells me I need a rooted
directory. If I try to get the physical directory name from
Server.MapPath, it throws an exception.

Is there any way to either get the physical path to a virtual directory
so I can pass it to FileUpload.SaveAs, or an alternative method to
upload/delete files to and from a virtual directory?

Thanks in advance.
 
P

Patrice

Server.MapPath should work. What exception do you have ? Could it be a
permission issue ?
 
G

Guest

It will have to be accessible at the file system level from the server to
upload it. You can use the MapPath() to convert the http path to a file
system path but the server has to have that file system path accessible to
it....
 
B

B Letts

Patrice said:
Server.MapPath should work. What exception do you have ? Could it be a
permission issue ?

I don't get an exception from Server.MapPath, but I also don't get the
correct physical path.

I set up a virtual directory in IIS. Let's say I call it
MyVirtualDirectory and it points to d:\MyFiles.

If I try to find the physical path of MyVirtualDirectory like so:

Server.MapPath("MyVirtualDirectory")

It returns my current application physical directory with
"MyVirtualDirectory" appended on the end, for example,
"C:\MyASPNETProjects\ThisProject\MyVirtualDirectory"

Maybe this is a setup issue? The ultimate goal is to allow the users to
upload files to wherever the virtual directory points - which may be a
directory on the same server, or a different one altogether.
 
P

Patrice

What if you try "/MyVirtualDirectory" or "~/MyVirtualDirectory" instead ?

IMO Server.MapPath resolves the root and then add the relative path
resolving the relative path to a local directory.
Using an absolute path should hopefully solve this issue.
 
P

Patrice

Gave this a try and it works fine here even if relative ?

Does this physical directory exists ? If you have both a virtual and a
physical directory Server.MapPath might well favor the physical directory ?

Else double check your IIS configuration ? Is the site restarted since you
created the virtual directory ? If you type the URL in your browser can you
check if you reach the expected location ?

Good luck.
 
B

B Letts

Curt_C said:
It will have to be accessible at the file system level from the server to
upload it. You can use the MapPath() to convert the http path to a file
system path but the server has to have that file system path accessible to
it....
That's fine. I assume the server has to have the file system path
accessible in order to set it up as a virtual directory to begin with,
correct?

What, then, is the format I need to pass to MapPath to get the physical
address of the virtual directory? Everything I try appends the physical
directory of my ASP.NET project to the virtual directory.

IE: I have the virtual directory set as MyVirtualDirectory, pointing to
D:\MyFiles.

My ASP.NET project lives in C:\MyProjects\Project_1

If I try Server.MapPath("MyVirtualDirectory"), I get back
C:\MyProjects\Project_1\MyVirtualDirectory.

I need to get D:\MyFiles.

What am I doing wrong?
 
B

B Letts

Curt_C said:
Try
Server.MapPath("/MyVirtualDirectory") and see what you get.

Nope. I get an InvalidOperationException if I try that.

Here's what I tried:

- Set up a Virtual Directory in IIS, called MyVirtualDirectory. It
points to C:\temp. I gave it all permissions - read write browse execute
etc.

- pointing a browser to http://localhost/MyVirtualDirectory/ gives me a
file listing of the correct directory

- My ASP.NET project that I'm trying to acccess the virtual directory is
running from C:\MyProject


Server.MapPath("MyVirtualDirectory") gives me
"C:\\MyProject\\MyVirtualDirectory"

Server.MapPath("\\MyVirtualDirectory") gives me an
InvalidOperationException

Server.MapPath("\MyVirtualDirectory") gives me an unrecognized escape
sequence (no surprise)

Server.MapPath("/MyVirtualDirectory") gives me InvalidOperationException
and also says "Failed to map the path '/MyVirtualDirectory'."}

Server.MapPath("//MyVirtualDirectory") gives me
InvalidOperationException and also says "Failed to map the path
'/MyVirtualDirectory'."}


What else can I try? Could this be a permissions and/or setup issue?
 
J

Juan T. Llibre

That's odd, using :

Server.MapPath("/MyVirtualDirectory") works fine for me.

Server.MapPath("/SomeOtherVirtualDirectory/") works fine, too.

Server.MapPath("/aDifferentVirtualDirectory/somefile.ext") also works.
 
G

Guest

Is MyVirtualDirectory a different application (not under your web
application root in IIS ) ?.. if so, I think for security reasons, it will
not be possible to get path of a different application..

In IIS, MyVirtualDirectory should be like below for Server.MapPath to work

Default WebSite --
|
-- your Web Application Root --
|
 
G

Guest

The sample path i put in prev post was broken coz of word wrap..

Is the MyVirtualDirectory configured like Scenario 1 or 2 in IIS?

Scenario 1 ) Default WebSite -> your Web Application Root ->
MyVirtualDirectory

Scenario 2)
Default WebSite -> your Web Application Root
Default WebSite -> MyVirtualDirectory
 
J

Juan T. Llibre

re:
Is MyVirtualDirectory a different application (not under your web
application root in IIS ) ?.. if so, I think for security reasons, it will
not be possible to get path of a different application..

Incorrect. Server.MapPath *does* work from different Applications.
 
G

Guest

you are right.. it works with syntax Server.MapPath("/MyVirtualDirectory") ,
even if a diffrent application..

Thanks for correcting..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top