How to upload Image using multipart in MVC application with example

As asp.net web forms MVC don't support Image or file upload functionality directly. You have to declare your Form enctype =multipart/form-data. Before read this article if you are new to MVC than you must read our previous article to create first MVC5 project. In this article we have perform simple yet useful asp.net mvc practical that will surely solve your image upload problem in MVC.
Form Syntax with multipart/form-data
@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { enctype = "multipart/form-data" }))
To perform this practical we make entity student. All basic fields are included and we will store image virtual path to ImageName string field as usually people do.
public class Student
{
   public int Id { get; set; }
   public string Name { get; set; }
   public string Email { get; set; }
   public string ImageName { get; set; }
}
Now we will write little html code in our View to create textbox for Name and Email, File Upload control for ImageName.
@using (Html.BeginForm("UploadImage", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  <div>
    @Html.LabelFor(model => model.Name)
    @Html.TextBoxFor(model => model.Name)
  </div>
  <br/>
  <div>
    @Html.LabelFor(model => model.Email)
    @Html.TextBoxFor(model => model.Email)
  </div>
  <br/>
  <div>
    @Html.LabelFor(model => model.ImageName)
    <input name="fileuploadImage" type="file" /><br /><br />
   </div>
   <br />
  <div>
    <input type="submit" value="Save" />
  </div>
}
Now, It's time to write Save Image code in our Controller. To Get all basic File Upload control Property we have to use HttpPostedFileBase. You will get several options like HttpPostedFileWrapper, ContentLength, ContentType, FileName and InputStream (where image data stored).The code look like below. You can write your own code to save image in folder.
[HttpPost]
public ActionResult UploadImage(Student student)
{
  HttpPostedFileBase file = Request.Files["fileuploadImage"];

  // write your code to save image
  string uploadPath = Server.MapPath("~/");
  file.SaveAs(uploadPath+ file.FileName);

  return View();
}
After running this practical , debug your code you will get similar output below.(see underline text)
Still Not clear then write comment below to discuss !
Happy Coding !