Upload file in ASP.NET MVC
Uploading files is a common task in websites. Let’s take a look on how we can
upload file in ASP.NET MVC.
View Code
<form
enctype="multipart/form-data" method="post">
<input type="file" name="fileUser" />
<input type="submit" value="Submit" />
</form>
Controller Code
[HttpPost]
public ActionResult
Index(HttpPostedFileBase fileUser)
{
if (fileUser != null)
{
fileUser.SaveAs(Path.Combine(HttpContext.Server.MapPath("~/UserImages"), fileUser.FileName));
}
return View();
}
Points to remember
- Form should have attribute enctype="multipart/form-data".
- File element should have a name attribute.
- Value of name attribute should be same as which we are receiving in controller. Otherwise it will be null.
Comments
Post a Comment