Click here to Skip to main content
11,929,675 members (55,799 online)
Click here to Skip to main content
Add your own
alternative version


72 bookmarked

Implementing HTTP File Upload with ASP.NET MVC

, 14 Aug 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
In this article, I will explore how to upload a file using ASP.NET MVC. Since the MVC framework does not use server controls, it will be interesting to see how file upload works in MVC.


In this article, I will explore how to upload a file using ASP.NET MVC. Since the MVC framework does not use server controls, it will be interesting to see how file upload works in MVC.

Using the code

Here is my view that renders a form for uploading files:

<%@ Page Title="" Language="C#" 
  Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
     <% using (Html.BeginForm("FileUpload", "FileUpload", 
                    FormMethod.Post, new { enctype = "multipart/form-data" }))
        <input name="uploadFile" type="file" />
        <input type="submit" value="Upload File" />
<%} %>

Now, I will write a controller for file upload, and here is my FileUploadController.cs:

public class FileUploadController : Controller
    public ActionResult FileUpload()
        return View();

    public ActionResult FileUpload(HttpPostedFileBase uploadFile)
        if (uploadFile.ContentLength > 0)
            string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), 
        return View();

One thing to note is that the controller has two overloaded methods of FileUpload. The first, ActionResult FileUpload(), is just to render the form and the method that is attributed with [AcceptVerbs(HttpVerbs.Post)] will upload the file. Here is the output form for uploading files:


The MVC framework does not use server controls, so we built an HTTP File Upload method with ASP.NET MVC.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Farooq Kaiser
Software Developer (Senior)
Canada Canada
12+ years of complete software development life cycle experience for web based applications and multi-tier client-server desktop, primarily using LINQ, WCF, WWF, C#, ASP.NET, XML, XSLT, AJAX, Winforms,Visual Basic, JavaScript, JQuery, Google APIs, C++, VB.NET, C, ATL/COM, Open XML. Extensively involved in the requirement analysis, feasibility study, conceptualization, planning, architecture/design, configuration, development, quality assurance, implementation and release of the software products.

You may also be interested in...

Comments and Discussions

QuestionI have to upload file size upto 4 GB. Pin
Member 1058738520-Apr-14 23:48
memberMember 1058738520-Apr-14 23:48 
GeneralI gave it 3 Pin
maxtyrann20-Nov-11 6:46
membermaxtyrann20-Nov-11 6:46 
GeneralMy vote of 1 Pin
Brady Kelly4-Jul-11 0:46
memberBrady Kelly4-Jul-11 0:46 
GeneralUpload large files Pin
ttvvaa17-Nov-10 23:40
memberttvvaa17-Nov-10 23:40 
GeneralMy vote of 5 Pin
VUnreal21-Sep-10 12:19
memberVUnreal21-Sep-10 12:19 
GeneralI have a problem using something similar to upload files Pin
vsrihari1629-Jun-10 2:14
membervsrihari1629-Jun-10 2:14 
GeneralSimple and to the point Pin
Dan Crowell16-Jun-10 11:17
memberDan Crowell16-Jun-10 11:17 
GeneralGood article Pin
Donsw11-Jun-10 9:36
memberDonsw11-Jun-10 9:36 
GeneralMy vote of 1 Pin
Miguel J4-Feb-10 13:31
memberMiguel J4-Feb-10 13:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 14 Aug 2009
Article Copyright 2009 by Farooq Kaiser
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid