Click here to Skip to main content
13,397,892 members (41,301 online)
Click here to Skip to main content
Add your own
alternative version

Stats

169.5K views
74 bookmarked
Posted 14 Aug 2009

Implementing HTTP File Upload with ASP.NET MVC

, 14 Aug 2009
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.

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)

Share

About the Author

Farooq Kaiser
Software Developer (Senior) http://www.Fairnet.com
Canada Canada
16+ years of complete software development life cycle experience for web based applications, SOA, IOS and distributed, primarily using ASP.NET, MVC, Entity Framework, WEBAPI 2, LINQ, WCF, C#, XML, XSLT, AJAX, Winforms, JavaScript, JQuery, Angular JS, Typescript, Google APIs, C/C++, VB.NET, JAVA, Open XML, SSIS,SSRS, Amazon Web Services, Microsoft Azure, Python, PHP, Open Source Frameworks and design patterns.

You may also be interested in...

Pro

Comments and Discussions

 
GeneralSimple and to the point Pin
Dan Crowell16-Jun-10 11:17
memberDan Crowell16-Jun-10 11:17 

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.

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