图像文件路径到服务器而不是C:驱动器MVC5

时间:2017-08-31 07:39:08

标签: c# image entity-framework asp.net-mvc-5

我有一个上传图片的应用程序,我试图获取存储在服务器而不是我的C:驱动器上的路径。下面是控制器中的代码:

 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);
}

目前正在这样存储(见上图)我需要的路径是" admin.loyaltyworx.co.za \ Images \ jeep.jpg"我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

使用

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

而不是

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