Server.MapPath Failed to map the path error

D

Dave

The below code is what I'm using to upload JPG's to a Shared folder on
my web server. The folder \\GLSDBS03\Entry$ is outside my Web folder.
When using FileUpload, and trying to save the JPG, I get a "Failed to
map the path '/GLSDBS03/Entry$'" error. I'm open to suggestions or a
better idea.

Thanks

*********************************************

public partial class ImageUpload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Boolean fileOK = false;
String path = Server.MapPath("\\\\GLSDBS03\\Entry$");
if (FileUpload1.HasFile)
{
String fileExtension =

System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] allowedExtensions =
{ ".gif", ".png", ".jpeg", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions)
{
fileOK = true;
}
}
}

if (fileOK)
{
try
{
FileUpload1.PostedFile.SaveAs(path +
FileUpload1.FileName);
ListBox1.Items.Add(FileUpload1.FileName);
Label1.Text = "File uploaded!";
}
catch (Exception ex)
{
Label1.Text = "File could not be uploaded.";
}
}
else
{
Label1.Text = "Cannot accept files of this type.";
}
}
else
{
DirectoryInfo di = new
DirectoryInfo("\\\\GLSDBS03\\Entry$");
try
{
// Determine whether the directory exists.
if (di.Exists)
{
FileInfo[] files = di.GetFiles();
for (int i = 0; i < files.Length; i++)
{
ListBox1.Items.Add(files.Name);
}
}
}
finally { }
}
}
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Well; to what you expect ot have that UNC maped to?
The idea of "mapping" is that from a web app you have a relative path like
/myfolder or / and if you need to use File methods you need the real path of
those folders, so in that case / could be traslated to c:\inetpub\www\app1

\\GLS.... is already a global path, you do not need to map it to nothing.

The only problem you will have is of permissions. usually a web app cannot
access nothing outside its root path. For obvious security options.

you may need to map your path under IIS
 
D

Dave

Ignacio,
Thanks for your reply. This is where it gets confusing. The
DirectoryInfo line below works fine. Also, the \\GLS address is to a
shared drive, so I'm still a bit at a loss.

A bit more history. The reason why the folder is out root path is so
that it doesn't get over written if/when changes are made to the web
site. The folder contains employee pictures.

In mapping the folder under IIS, if I understand you correctly, you're
suggesting that I set it up with it's own http://?


Thanks for your time


Hi,

Well; to what you expect ot have that UNC maped to?
The idea of "mapping" is that from a web app you have a relative path like
/myfolder or / and if you need to use File methods you need the real path of
those folders, so in that case / could be traslated to c:\inetpub\www\app1

\\GLS.... is already a global path, you do not need to map it to nothing.

The only problem you will have is of permissions. usually a web app cannot
access nothing outside its root path. For obvious security options.

you may need to map your path under IIS


--
Ignacio Machin
machin AT laceupsolutions com



Dave said:
The below code is what I'm using to upload JPG's to a Shared folder on
my web server. The folder \\GLSDBS03\Entry$ is outside my Web folder.
When using FileUpload, and trying to save the JPG, I get a "Failed to
map the path '/GLSDBS03/Entry$'" error. I'm open to suggestions or a
better idea.

Thanks

*********************************************

public partial class ImageUpload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Boolean fileOK = false;
String path = Server.MapPath("\\\\GLSDBS03\\Entry$");
if (FileUpload1.HasFile)
{
String fileExtension =

System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] allowedExtensions =
{ ".gif", ".png", ".jpeg", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions)
{
fileOK = true;
}
}
}

if (fileOK)
{
try
{
FileUpload1.PostedFile.SaveAs(path +
FileUpload1.FileName);
ListBox1.Items.Add(FileUpload1.FileName);
Label1.Text = "File uploaded!";
}
catch (Exception ex)
{
Label1.Text = "File could not be uploaded.";
}
}
else
{
Label1.Text = "Cannot accept files of this type.";
}
}
else
{
DirectoryInfo di = new
DirectoryInfo("\\\\GLSDBS03\\Entry$");
try
{
// Determine whether the directory exists.
if (di.Exists)
{
FileInfo[] files = di.GetFiles();
for (int i = 0; i < files.Length; i++)
{
ListBox1.Items.Add(files.Name);
}
}
}
finally { }
}
}
}
 
D

Dave

Got it! In IIS, at the web site, create a New Virtual Directory
pointing to the folder. At that point it makes it as part of the root.


Thanks Ignacio!


Ignacio,
Thanks for your reply. This is where it gets confusing. The
DirectoryInfo line below works fine. Also, the \\GLS address is to a
shared drive, so I'm still a bit at a loss.

A bit more history. The reason why the folder is out root path is so
that it doesn't get over written if/when changes are made to the web
site. The folder contains employee pictures.

In mapping the folder under IIS, if I understand you correctly, you're
suggesting that I set it up with it's own http://?


Thanks for your time


Hi,

Well; to what you expect ot have that UNC maped to?
The idea of "mapping" is that from a web app you have a relative path like
/myfolder or / and if you need to use File methods you need the real path of
those folders, so in that case / could be traslated to c:\inetpub\www\app1

\\GLS.... is already a global path, you do not need to map it to nothing.

The only problem you will have is of permissions. usually a web app cannot
access nothing outside its root path. For obvious security options.

you may need to map your path under IIS


--
Ignacio Machin
machin AT laceupsolutions com



Dave said:
The below code is what I'm using to upload JPG's to a Shared folder on
my web server. The folder \\GLSDBS03\Entry$ is outside my Web folder.
When using FileUpload, and trying to save the JPG, I get a "Failed to
map the path '/GLSDBS03/Entry$'" error. I'm open to suggestions or a
better idea.

Thanks

*********************************************

public partial class ImageUpload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Boolean fileOK = false;
String path = Server.MapPath("\\\\GLSDBS03\\Entry$");
if (FileUpload1.HasFile)
{
String fileExtension =

System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] allowedExtensions =
{ ".gif", ".png", ".jpeg", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions)
{
fileOK = true;
}
}
}

if (fileOK)
{
try
{
FileUpload1.PostedFile.SaveAs(path +
FileUpload1.FileName);
ListBox1.Items.Add(FileUpload1.FileName);
Label1.Text = "File uploaded!";
}
catch (Exception ex)
{
Label1.Text = "File could not be uploaded.";
}
}
else
{
Label1.Text = "Cannot accept files of this type.";
}
}
else
{
DirectoryInfo di = new
DirectoryInfo("\\\\GLSDBS03\\Entry$");
try
{
// Determine whether the directory exists.
if (di.Exists)
{
FileInfo[] files = di.GetFiles();
for (int i = 0; i < files.Length; i++)
{
ListBox1.Items.Add(files.Name);
}
}
}
finally { }
}
}
}
 
D

Dave

Got it! In IIS, at the web site, create a New Virtual Directory
pointing to the folder. At that point it makes it as part of the root.


Thanks Ignacio!


Ignacio,
Thanks for your reply. This is where it gets confusing. The
DirectoryInfo line below works fine. Also, the \\GLS address is to a
shared drive, so I'm still a bit at a loss.

A bit more history. The reason why the folder is out root path is so
that it doesn't get over written if/when changes are made to the web
site. The folder contains employee pictures.

In mapping the folder under IIS, if I understand you correctly, you're
suggesting that I set it up with it's own http://?


Thanks for your time


Hi,

Well; to what you expect ot have that UNC maped to?
The idea of "mapping" is that from a web app you have a relative path like
/myfolder or / and if you need to use File methods you need the real path of
those folders, so in that case / could be traslated to c:\inetpub\www\app1

\\GLS.... is already a global path, you do not need to map it to nothing.

The only problem you will have is of permissions. usually a web app cannot
access nothing outside its root path. For obvious security options.

you may need to map your path under IIS


--
Ignacio Machin
machin AT laceupsolutions com



Dave said:
The below code is what I'm using to upload JPG's to a Shared folder on
my web server. The folder \\GLSDBS03\Entry$ is outside my Web folder.
When using FileUpload, and trying to save the JPG, I get a "Failed to
map the path '/GLSDBS03/Entry$'" error. I'm open to suggestions or a
better idea.

Thanks

*********************************************

public partial class ImageUpload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Boolean fileOK = false;
String path = Server.MapPath("\\\\GLSDBS03\\Entry$");
if (FileUpload1.HasFile)
{
String fileExtension =

System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] allowedExtensions =
{ ".gif", ".png", ".jpeg", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions)
{
fileOK = true;
}
}
}

if (fileOK)
{
try
{
FileUpload1.PostedFile.SaveAs(path +
FileUpload1.FileName);
ListBox1.Items.Add(FileUpload1.FileName);
Label1.Text = "File uploaded!";
}
catch (Exception ex)
{
Label1.Text = "File could not be uploaded.";
}
}
else
{
Label1.Text = "Cannot accept files of this type.";
}
}
else
{
DirectoryInfo di = new
DirectoryInfo("\\\\GLSDBS03\\Entry$");
try
{
// Determine whether the directory exists.
if (di.Exists)
{
FileInfo[] files = di.GetFiles();
for (int i = 0; i < files.Length; i++)
{
ListBox1.Items.Add(files.Name);
}
}
}
finally { }
}
}
}
 

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

Top