Create a new Controller named FileUploadController and add two action result like below
public class FileUploadController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult UploadFile()
{
var httpPostedFileBase = Request.Files["FileName"];
if (httpPostedFileBase != null && httpPostedFileBase.ContentLength > 0)
{
string extension = System.IO.Path.GetExtension(httpPostedFileBase.FileName);
string path1 = string.Format("{0}/{1}", Server.MapPath("~/SavedFiles"), extension);
if (System.IO.File.Exists(path1))
System.IO.File.Delete(path1);
httpPostedFileBase.SaveAs(path1);
}
ViewData["Status"] = "Success";
return View("Index");
}
}
Right click the Index action and create an index view like below
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@Html.Partial("_FileUpload")
</div>
</body>
</html>
Right click FileUpload folder inside Views folder and create a Partial View(You can check partial view checkbox from the window) named _FileUpload and paste the code like below
@using(Html.BeginForm("UploadFile","FileUpload",FormMethod.Post,new{enctype="multipart/form-data"}))
{
<input type="file" name="FileName" id="file" style="width:240px"/>
<input type="submit" value="Upload"/>
}
You can call this partial view in multiple pages like we have called in index view
Hope this helps