troubleshooting Question

Failed to map the path

Avatar of -Dman100-
-Dman100-Flag for United States of America asked on
.NET ProgrammingASP.NET
4 Comments1 Solution3481 ViewsLast Modified:
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;
            }
        }
    }
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros