Upload File in ASP.NET MVC
28-12-2015 17:36:26
C# / ASP.NET MVC
0 Bookmark(s)
210 View(s)
Code for the view:
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
}
Code for the controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var savePath = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(savePath);
}
return RedirectToAction("Index");
}
}