user4857063
user4857063

Reputation:

Image File path to server not C: drive MVC5

I have an application that uploads images, I am trying to get the path to store on the server and not on my C:drive. Below is the code in the controller:

 if (ModelState.IsValid)
        {

        if (file != null)
        {
            string ImageName = System.IO.Path.GetFileName(file.FileName);
            string physicalPath = Server.MapPath("~/Images/");

            try
            {
                if (!Directory.Exists(physicalPath))
                    Directory.CreateDirectory(physicalPath);

                string physicalFullPath = Path.Combine(physicalPath, ImageName);

                file.SaveAs(physicalFullPath);

                customer.CustomerLogo = ImageName;
                customer.CustomerLogoPath = physicalFullPath;
                db.Customers.Add(customer);
                db.SaveChanges();
            }
            catch(Exception e)
            {
                return View("Error",e.Message );
            }
        }
        return RedirectToAction("Index");

    }
    return View(customer);
}

It's currently being stored like this(See above Image) I need the path to be "admin.loyaltyworx.co.za\Images\jeep.jpg" How can I achieve this?

Upvotes: 2

Views: 790

Answers (1)

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34180

Use

string physicalPath = "c:\\admin.loyaltyworx.co.za\\Images";

Instead of

string physicalPath = Server.MapPath("~/Images/");

Upvotes: 1

Related Questions