I am trying to upload multiple files, (selecting multiple files in a single click and upload). For that I am using the following code. I am doing this in MVC4
@using (Html.BeginForm("Gallery", "Admin", FormMethod.Post, new {enctype="multipart/form-data", id = "GalleryForm" }))
{
@Html.ValidationSummary();
<div> Select the files to Upload<br /> <input type="file" name="file" id="file" multiple="multiple" /><br /><br /></div>
<div><input type="submit" name="submit" Value="Save"/></div>
}
Controller
[HttpPost]
public ActionResult Gallery(IEnumerable<HttpPostedFileBase> files)
{
foreach (var file in files)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Images/Gallery/"), fileName);
file.SaveAs(path);
}
}
return RedirectToAction("Index");
}
If I select more than one file, I got the error "Maximum request length exceeded" and when I select single file and try to upload then, I got the error "Object reference not set to an instance of an object"
. Actually, I want to upload single and multiple files using this same form. How will this be possible. Please help me. Thanks in advance.