HTTP File Upload with ASP.NET MVC 3
In this article, I will look at how to upload a file using ASP.NET MVC3.
View for Interface:-
Now we need a View to Give the User Interface for upload a file in this application.
ViewBag.Title = "Http File Upload";
<form method="post" enctype="multipart/form-data">
<label for="file">Upload Image:</label>
<input type="file" name="file" id="file"/ >
<input type="submit" value="Upload Image"/>
· After written above code the output will below.
public class HomeController : Controller
// GET: /Home/
public ActionResult Index()
[HttpPost] //this line for Form method POST in Index.cshtml
public ActionResult Index(HttpPostedFileBase file) 
var fileName = Path.GetFileName(file.FileName);
if (fileName != null) 
file.SaveAs(Server.MapPath("~\\files\\" + fileName)); 
. That the argument to the Index action method is an instance(file) of
. Here is declare a fileName variable for contain the file name which you want to upload. GetFileName() is a static method of Path class which is get the file name from file instance through the FileName property.
. Here is check fileName is not null if statement will return true then new line execute.
. SaveAs() a method of HttpPostedFileBaseclass Which is save the file in our application folder which is given location in Server.MapPath() method.
· Now press Ctrl+F5 for Run the Application.
· Now select any file for upload through the Browse... button.
· After selected file click Upload File... Button.
· Now Goto on the solution Explorer and Refresh your Solution Explorer and see your uploaded file in files folder with selected file name.