Failed to map the path

-Dman100-
-Dman100- used Ask the Experts™
on
I have a simple file upload page that allows a user to upload files, add a description and it inserts the record into the database (sql server 205) and then saves the file to a virtual directory called "FileUpload" that I created in IIS.  It is pretty straightforward.

The file upload is not working on my development PC.  We've tried the file upload code on another development PC and it works fine.

The problem seems to be how I configured the virtual directory "FileUpload" in IIS on my local development PC.  When I user Server.MapPath it does not seem to find the path to that directory.

The database inserts are working fine.  The only problem is the file is not getting saved to disk in the "FileUpload" virtual directory I created.

The path to the virtual directory is not located on my c: drive.  It is located on another local drive on my PC, which is my g: drive.

I'm having a difficult time troubleshooting this error.

The error appears to be occurring on this line:
fileUp.PostedFile.SaveAs(Server.MapPath(NewFile.Path + NewFile.SysFileName));

See the entire code-behind for my file upload page below.

Any help is appreciated.
Thanks.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CompassLearningInternal.DAL;
using CompassLearningInternal.Model;
 
public partial class Controls_InformationCenter : System.Web.UI.UserControl
{
    string FileSavePath = ConfigurationManager.AppSettings["FileUploadPath"].ToString();
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.ddlCategories.DataSource = InformationCenterDAO.GetAllInformationCenterCategories();
            this.ddlCategories.DataBind();
            this.ddlCategories.Items.Insert(0, new ListItem("----- Please Select -----", ""));
 
 
        }
 
        if (Page.IsPostBack)
        {
            if (ddlCategories.SelectedValue != "")
            {
                this.grvFiles.DataSource = InformationCenterDAO.GetInformationCenterFilesByCategoryId(Int32.Parse(ddlCategories.SelectedValue));
                this.grvFiles.DataBind();
                pnlContent.Visible = true;
            }
            else
            {
                this.grvFiles.Controls.Clear();
                pnlContent.Visible = false;
            }
        }
    }
 
    protected void grvFiles_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Enable/disable active column
            DataRowView rowView = (DataRowView)e.Row.DataItem;
            Literal litPath = (Literal)e.Row.FindControl("litPath");
 
            if (rowView["path"].ToString() != "")
            {
                litPath.Text = "<a href='" + Page.ResolveUrl(@rowView["path"].ToString() + rowView["system_file_name"].ToString()) + "' target='_new' />" + rowView["name"].ToString() + "</a>";
            }
        }
    }
 
    protected void grvFiles_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "RowDeleting")
        {
            int delete = InformationCenterDAO.DeleteFile(Int32.Parse(e.CommandArgument.ToString()));
 
            if (delete != -1)
            {
                lblConfirmation.Text = "File successfully deleted from Information Center.";
            }
            else
            {
                lblConfirmation.Text = "There was a problem removing the file from the Information Center.";
            }
        }
    }
 
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        //get path on client
        string strFileName = System.IO.Path.GetFileName(fileUp.PostedFile.FileName);
 
        string[] exts = { "pdf", "doc", "xls", "ppt", "gif", "jpg", "tif", "bmp", "txt" };
        bool acceptFile = false;
 
        //filter out non-allowed extensions
        foreach (string ext in exts)
        {
            if (strFileName.IndexOf(ext) != -1) acceptFile = true;
        }
 
        if (!acceptFile)
        {
            //Alert - wrong file type
            lblConfirmation.Text = "File is not an acceptable format. Upload failed";
            return;
        }
        else
        {
            try
            {
                int category_id = -1;
                category_id = Int32.Parse(ddlCategories.SelectedValue);
 
                //Save file to DB, then add to disk
                CompassFile NewFile = InformationCenterDAO.InsertFile(category_id, strFileName.Trim(), FileSavePath.Trim(), txtDescription.Text, "");
 
                if (NewFile.ID != 0)
                {
                    //Save to disk
                    fileUp.PostedFile.SaveAs(Server.MapPath(NewFile.Path + NewFile.SysFileName));
 
                    //Alert success
                    lblConfirmation.Text = "File successfully added to Information Center.";
 
                    //reset all fields
                    //ResetMicroSiteFields();
                    //BindItems(Int32.Parse(ddlStates.SelectedValue));
                }
                else
                {
                    //Alert - wrong file type
                    lblConfirmation.Text = "File could not be added to Information Center.";
 
                    return;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
are proper permissions for the asp.net and iis users are set on the "FileUpload" folder ??

If not then give read/write permissions to asp.net user and iis user for the specified directory.
-Dman100-Software Consultant

Author

Commented:
Hi ajaysharmaapis,

Thanks for replying to my post.

I'm using Visual Studio 2005 built-in web server (WebDev.WebServer.exe) to test and debug my application.  I am running XP Professional and have IIS installed on my development PC.  I created a virtual directory "FileUpload" that points to the following physical path: G:\MySolutionName\Projects\WebApps\FileUpload.

What I am trying to do thru the application is upload files to the the path of the virtual directory I created...i.e."FileUpload".

Whenever I try to upload a file, I get the error "failed to map path".

As a test, I tried the use MapPath in the page load event to see if I could translate the relative path to the physical path, but I'm still getting the error "failed to map path".

string a = HttpContext.Current.Server.MapPath("/FileUpload/testfile.pdf");
Response.Write(a);

So, the problem is Server.MapPath is unable to translate the relative path to the full physical path.  I cannot figure out why?

Any help on this is appreciated.

Thanks
After going threw some websites i found one thing that you should use ~ to point to root directory.

Same problem happened with me and i ended up with same thing. I put ~ in front of every path and removed ".." (used for directing to parent folder).

These link should be helpful:

http://www.theserverside.net/discussions/thread.tss?thread_id=39625

http://geekswithblogs.net/vivek/archive/2007/01/21/104101.aspx
-Dman100-Software Consultant

Author

Commented:
It appears the problem is with Visual Studio internal web server?  If I setp the the site in IIS, then when I run the page, the file upload works fine and wrties the file to the virtual directory "FileUpload".

If I run the site using the internal Visual Studio web server, I get the failed to map path error.  Visual Studio's web server does not seem to be able to look outside of the site structure to another virtual directory running in IIS?  That is what appears to be the problem anyway?  I'm not 100% sure.

Does this sound correct to anyone?  I've tried debugging, response writing out all the values, etc.  Everything looks correct.  And it works under IIS, jut not the local Visual Studio web server.  Is this a limitation of the internal web server that ships with Visual Studio 2005?  Is there a setting or configuration that I'm missing in Visual Studio to allow this type of file path mapping?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial