-2

In this code, an error occurs when I save a file to disk.

if (ModelState.IsValid)
{
    if (account.file != null)
    {
        string FileName = Path.GetFileName(account.file.FileName);
        //Save files to disk
        account.file.SaveAs(Server.MapPath("Uploads/" + FileName));

        //Add Entry to DataBase
        string conection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(conection))
        {

            string InsertAccount = "Insert into tblUser (FirstName,LastName,EmailAdress,UserName,Password,UserImage) values(@fname,@lname,@emailadress,@username,@password,@Userimagepath)";
            SqlCommand cmd = new SqlCommand(InsertAccount, con);
            cmd.Parameters.AddWithValue("@fname", account.FirstName);
            cmd.Parameters.AddWithValue("@lname", account.LastName);
            cmd.Parameters.AddWithValue("@emailadress", account.EmailAddress);
            cmd.Parameters.AddWithValue("@username", account.Username);
            cmd.Parameters.AddWithValue("@password", account.Password);
            cmd.Parameters.AddWithValue("@UserimagePath", "Uploads/" + FileName);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }

    }
}

Could not find a part of the path E:\Projects\FBClone\FBClone\Account\Uploads\14494605_1912172235673452_6934911680009623037_n.jpg. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path E:\Projects\FBClone\FBClone\Account\Uploads\14494605_1912172235673452_6934911680009623037_n.jpg.

Source Error:

Line 30: string FileName = Path.GetFileName(account.file.FileName);
Line 31: //Save files to disk
Line 32: account.file.SaveAs(Server.MapPath("Uploads/" + FileName));
Line 33: 
Line 34: //Add Entry to DataBase

Source File: E:\Projects\FBClone\FBClone\Controllers\AccountController.cs Line: 32

Joe Wilson
  • 5,591
  • 2
  • 27
  • 38

1 Answers1

0

Looking at this SO link, it appears that you need a tilde in front of Uploads:

account.file.SaveAs(Server.MapPath("~Uploads/" + FileName));
Phil N DeBlanc
  • 398
  • 1
  • 13