public ActionResult test() { return View(); } [HttpPost] public ActionResult test(HttpPostedFileBase file) { if (file == null) { return View("empty"); } else if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath("~/image/"), fileName); file.SaveAs(path); return View(); } else { return View("ok"); } return RedirectToAction("Index"); }
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>test</title> </head> <body> <div> @ViewBag.msg; @using (Html.BeginForm("test", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { <input type="file" id="file" /> <input type="submit" /> } </div> </body> </html>
<input type="file" id="file" name="file" />
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)