So how to do this?? Lets get start. Create a new MVC 4.0 application and add a new controller, name it as HomeController. We will use Index ActionMethod to write the code to upload the file.

We need two ActionMethod named Index, one is for HttpGet and another for HttpPost. Within the HttpGet ActionMethod we don't need to write anything.

Lets create the View first. To create the View right click on the ActionMethod Index and click on the Add View option.

In the View write down the code.

@{

ViewBag.Title ="Upload file";

}

<h2>Upload
File</h2>

<h3style="color:green">@ViewBag.Message</h3>

@using(Html.BeginForm("Index","Home",FormMethod.Post

,new{ enctype ="multipart/form-data"}))

{

@Html.ValidationSummary();

<inputtype="file"id="fileToUpload"name="file"/>

<spanclass="field-validation-error"id="spanfile"></span>

<inputtype="submit"id="btnSubmit"value="Upload"/>

}

Here we have taken a simple HTML file up loader and a submit button. Within the form we are calling the ActionMethod Index, which is present in HomeController. A ValidationSummary to show all validation message.

Now get back to the ActionMethod. Within the Index ActionMethod (HttpPost) write down the code.