Click here to Skip to main content
Click here to Skip to main content

Implementing HTTP File Upload with ASP.NET MVC

By , 14 Aug 2009
 

Introduction

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#" 
  MasterPageFile="~/Views/Shared/Site.Master" 
  Inherits="System.Web.Mvc.ViewPage" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
            FileUpload
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>FileUpload</h2>
    
     <% using (Html.BeginForm("FileUpload", "FileUpload", 
                    FormMethod.Post, new { enctype = "multipart/form-data" }))
        {%>
        <input name="uploadFile" type="file" />
        <input type="submit" value="Upload File" />
<%} %>
 
</asp:Content>

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

[HandleError]
public class FileUploadController : Controller
{
    public ActionResult FileUpload()
    {
        return View();
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult FileUpload(HttpPostedFileBase uploadFile)
    {
        if (uploadFile.ContentLength > 0)
        {
            string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), 
                                           Path.GetFileName(uploadFile.FileName));
            uploadFile.SaveAs(filePath);
        }
        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:

Summary

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

License

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) http://www.Fairnet.com
Canada Canada
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralI gave it 3membermaxtyrann20 Nov '11 - 5:46 
GeneralMy vote of 1memberBrady Kelly3 Jul '11 - 23:46 
GeneralUpload large filesmemberttvvaa17 Nov '10 - 22:40 
GeneralMy vote of 5memberVUnreal21 Sep '10 - 11:19 
GeneralI have a problem using something similar to upload filesmembervsrihari1629 Jun '10 - 1:14 
I have a button on View page, which will open a dialog(modal) using JQuery script. This dialog contains few fields and the input type 'file' to select the file from the machine.
 
As this is a dialog box i am using Ajax call to post the data back to my MVC application(contoller/action)
Looks like Ajax calls wont deal with File Upload.
 
How to do File Upload in Jquery Dialog box in ASP.NET MVC?
 
Any help in achieving the same would be appreciated.
GeneralSimple and to the pointmemberDan Crowell16 Jun '10 - 10:17 
GeneralGood articlememberDonsw11 Jun '10 - 8:36 
GeneralMy vote of 1memberMiguel J4 Feb '10 - 12:31 

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

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