How to Upload File in ASP.NET MVC 3.0?

Total Post:341

C#  Mvc3  File  Upload 
 1496  View(s)
Rate this:
I want to upload file in mvc. How can I upload the file using html input file control?
  1. Post:134

    Re: How to Upload File in ASP.NET MVC 3.0?

    You don't use a file input control. Server side controls are not used in ASP.NET MVC.

    So you would start by creating an HTML form which would contain a file input:

    @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
        <input type="file" name="file" />
        <input type="submit" value="OK" />

    and then you would have a controller to handle the upload:

    public class HomeController : Controller
        // This action renders the form
        public ActionResult Index()
            return View();
        // This action handles the form POST and the upload
        public ActionResult Index(HttpPostedFileBase file)
            // Verify that the user selected a file
            if (file != null && file.ContentLength > 0)
                // extract only the fielname
                var fileName = Path.GetFileName(file.FileName);
                // store the file inside ~/App_Data/uploads folder
                var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
            // redirect back to the index action to show the form once again
            return RedirectToAction("Index");       

      Modified On Apr-06-2018 06:47:29 AM


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.