Thursday, 26 May 2016

MVC - Fix error The name 'Path' does not exist in the current context

Watch this example on YouTube:
to fix it replace 
        public ActionResult ImportFile(HttpPostedFileBase file)
        {
            if (Request.Files.Count > 0)
            {
                var fil = Request.Files[0];
                if (fil != null && fil.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/Images/"), fileName);
                    file.SaveAs(path);
                }
            }
            return View();
with
        public ActionResult ImportFile(HttpPostedFileBase file)
        {
            if (Request.Files.Count > 0)
            {
                var fil = Request.Files[0];
                if (fil != null && fil.ContentLength > 0)
                {
                    var fileName = System.IO.Path.GetFileName(file.FileName);
                    var path = System.IO.Path.Combine(Server.MapPath("~/Images/"), fileName);
                    file.SaveAs(path);
                }
            }
            return View();

No comments:

Post a Comment