Tuesday, April 5, 2016

Upload File In MVC

Add the file tag in the view.

@using(Html.BeginForm("SaveFile", "Employee", null, FormMethod.Post, new { enctype="multipart/form-data" }))
{
<input type="file" name="file" required />
<input type="submit" value="Submit" />
}

EmployeeController

[HttpPost]
public class SaveFile()
{
if (Request.Files.Count > 0)
{
var file = Request.Files[0];
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Employee/Images/"), fileName);
file.SaveAs(path);
}
}
return RedirectToAction("Index", "Employee");
}

No comments:

Post a Comment