Click here to Skip to main content
15,397,385 members
Articles / Web Development / XHTML
Article
Posted 14 Aug 2009

Stats

186.6K views
74 bookmarked

Implementing HTTP File Upload with ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
4.46/5 (23 votes)
14 Aug 2009CPOL
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.

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:

ASP.NET
<%@ 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:

C#
[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:

Image 1

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)

Share

About the Author

Farooq Kaiser
Software Developer (Senior) http://www.Fairnet.com
Canada Canada
No Biography provided

Comments and Discussions

 
QuestionI have to upload file size upto 4 GB. Pin
Member 1058738520-Apr-14 22:48
MemberMember 1058738520-Apr-14 22:48 
GeneralI gave it 3 Pin
maxtyrann20-Nov-11 5:46
Membermaxtyrann20-Nov-11 5:46 
GeneralMy vote of 1 Pin
Brady Kelly3-Jul-11 23:46
MemberBrady Kelly3-Jul-11 23:46 
GeneralUpload large files Pin
ttvvaa17-Nov-10 22:40
Memberttvvaa17-Nov-10 22:40 
GeneralMy vote of 5 Pin
Vladimir Svyatski21-Sep-10 11:19
professionalVladimir Svyatski21-Sep-10 11:19 
GeneralI have a problem using something similar to upload files Pin
vsrihari1629-Jun-10 1:14
Membervsrihari1629-Jun-10 1:14 
GeneralSimple and to the point Pin
Dan Crowell16-Jun-10 10:17
MemberDan Crowell16-Jun-10 10:17 
GeneralGood article Pin
Donsw11-Jun-10 8:36
MemberDonsw11-Jun-10 8:36 
Good article but you can go a little more in depth on the issues as an example of why there are two overloads. I know what you said but you can explain more. keep it coming. good work.
cheers,
Donsw
My Recent Article : CDC - Change Data Capture

GeneralMy vote of 1 Pin
Miguel J4-Feb-10 12:31
MemberMiguel J4-Feb-10 12: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.