ASP .Net MVC 3 File Upload

Doing File Upload in ASP .Net MVC 3 is easier than ASP .Net Web Form. On this example Only two controller and one view is needed to perform this task.


public class FileController : Controller
        // GET: /File/
        public ActionResult Index()
            return View();
        public ActionResult Upload(HttpPostedFileBase f)
            if (f.ContentLength > 0)
                string filePath = Path.Combine(HttpContext.Server.MapPath(“\\Uploads\\”), Path.GetFileName(f.FileName));
            return RedirectToAction(“Index”);
    ViewBag.Title = “Index”;
@using (Html.BeginForm(“Upload”, “File”, FormMethod.Post, new { enctype = “multipart/form-data” }))
    <input type=”file” name=”f” /><br />
    <input type=”submit” value=”Submit” />
Thats it. 
Download the source Here

public class FileController : Controller    {        //        // GET: /File/
        public ActionResult Index()        {            return View();        }        [HttpPost]        public ActionResult Upload(HttpPostedFileBase f)        {            if (f.ContentLength > 0)            {                string filePath = Path.Combine(HttpContext.Server.MapPath(“\\Uploads\\”), Path.GetFileName(f.FileName));                f.SaveAs(filePath);                            }            return RedirectToAction(“Index”);        }

Author: webmaster

I am Thomie Jose San Agustin a developer using PHP as my main programming language to create website. I can also develop other applications and websites using other programming languages such as VB .Net, C# .Net, Java, ASP .Net. I can also use Applications such as Photoshop, Flash, Adobe After Effects, Dreamweaver, etc.

5 thoughts on “ASP .Net MVC 3 File Upload”

  1. Pingback: Bronzeskulpturen
  2. Pingback: nachfolger

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.