public ActionResult Create(ViewModel p, HttpPostedFileBase file) { if (ModelState.IsValid && file != null && file.ContentLength > 0) { // Extract only the filename var fileName = System.IO.Path.GetFileName(file.FileName); // Store the file inside ~/App_Data/Data folder var path = System.IO.Path.Combine(Server.MapPath("~/App_Data/Data"), fileName); // Upload file file.SaveAs(path); p.SetPost.ByUser = AccountDAO.Id; p.SetPost.Photo = path; // Store file path to database if (ArticleDAO.Create(p.SetPost)) { ViewBag.Msg = "Article was created successfully."; return(RedirectToAction("Index", "Article")); } } ModelState.AddModelError("", "Can not create an article."); return(View()); }