Handle multiple files upload in MVC3

To handle multiple files upload, we can pass a IEnumerable<HttpPostedFileBase> to our controller action method.

        [HttpPost]
        public ActionResult Create(IEnumerable files)
        {
             foreach (var file in files)
            {
                if (file != null && file.ContentLength > 0)
                {
                    
                    var fileName = Path.GetFileName(file.FileName);
                }
            return View();
        }

In the view you will have multiple file upload inputs with the same name.

@using(Html.BeginForm("Action","Controller", new { id = Model}, FormMethod.Post,new { enctype = "multipart/form-data"})){
      <div class="editor-label">
          
      </div>
      <div class="editor-label">
          
      </div>
      <div class="editor-label">
          
      </div>
      <div class="editor-label">
          
      </div>
      
}

Mvc will handle the rest for you.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s