The following post captures the implementation details for uploading file in ASP.NET MVC.
First we need to create HTML form which would receive the file as input:
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<in put type="file" name="file" />
<in put type="submit" value="OK" />
}
Now, we create action for file upload, below is the code:
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
// Verify that the user selected a file
if (file != null && file.ContentLength > 0)
{
var name = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/uploads"), name);
file.SaveAs(path);
}
}
0 Comment(s)