Use below code and change path as per your requirements.
1. View
@using (Html.BeginForm("UploadExcel", "UserDetails", FormMethod.Post, new { enctype = "multipart/form-data" })) // ("Method name", "Controller name",post, type)
{
@Html.ValidationSummary(true)
@Html.LabelFor(model => model.Files)
<input type="file" name="fileupload" multiple="multiple" required="required"/>
@Html.ValidationMessageFor(model => model.Files)
<input type="submit" value="Create" />
}
2. Controller
[HttpPost]
public ActionResult UploadExcel(HttpPostedFileBase[] fileupload)
{
HttpPostedFileBase file = fileupload;
if (file == null)
{
ModelState.AddModelError("File", "Please Upload Your file");
}
else if (file.ContentLength > 0)
{
int MaxContentLength = 1024 * 1024 * 3; //3 MB
string[] AllowedFileExtensions = new string[] { ".jpg", ".gif", ".png", ".pdf" };
if (!AllowedFileExtensions.Contains(file.FileName.Substring(file.FileName.LastIndexOf('.'))))
{
ViewBag.Message = "File, Please file of type: " + string.Join(", ", AllowedFileExtensions);
}
else if (file.ContentLength > MaxContentLength)
{
ViewBag.Message = "File, Your file is too large, maximum allowed size is: " + MaxContentLength + " MB";
}
else
{
//TO:DO
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Content/Upload"), fileName);
file.SaveAs(path);
ModelState.Clear();
ViewBag.Message = "File uploaded successfully";
}
}
return View();
}