In this article I will explain with an example, how to upload file without clicking Submit Button using HTML FileUpload element (INPUT FILE) in ASP.Net MVC.
The
Submit Button will be hidden and as soon as File is selected, the
Submit Button click event is triggered using
JavaScript which will submit the Form and file is uploaded.
Namespaces
You will need to import the following namespace.
Controller
The Controller consists of following Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
Action method for handling POST operation
Inside this Action method, the HttpPostedFileBase class is passed as a parameter.
Note: The name of the HttpPostedFileBase parameter and the name of HTML FileUpload element must be exact same, otherwise the HttpPostedFileBase parameter will be NULL.
Then, a check is performed whether Directory (Folder) exists if not then the Directory (Folder) is created and then the file is saved to the Directory (Folder).
Finally, a Success Message is set in the
ViewBag object and returned to the
Index View.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Upload(HttpPostedFileBase postedFile)
{
if (postedFile != null)
{
string path = Server.MapPath("~/Uploads/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
postedFile.SaveAs(path + Path.GetFileName(postedFile.FileName));
ViewBag.Message = "File uploaded successfully.";
}
return View("Index");
}
}
View
HTML Markup
The View consists of HTML Form which has been created using the Html.BeginForm method with the following parameters.
ActionName – Name of the Action. In this case it is Submit.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
HtmlAttributes – This array allows to specify the additional Form Attributes. Here we need to specify enctype = “multipart/form-data” which is necessary for uploading Files.
The Form consists of HTML FileUpload element, a
Submit Button and a SPAN element for displaying
ViewBag Message.
The HTML FileUpload element has been specified with a
JavaScript onchange event handler and the
Submit Button is hidden.
UploadFile JavaScript function
The
UploadFile JavaScript function accepts FileUpload element as a parameter.
Inside
UploadFile JavaScript function, a check is perform if the FileUpload element has selected file then it will trigger the
click event of the
Submit Button.
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" id="fuUpload" name="postedFile" onchange="UploadFile(this)" />
<br />
<input id="btnUpload" type="submit" value="Upload" style="display: none" />
<br />
<span style="color:green">@Html.Raw(ViewBag.Message)</span>
}
<script type="text/javascript">
function UploadFile(postedFile) {
if (postedFile.value != '') {
document.getElementById("btnUpload").click();
}
}
</script>
</body>
</html>
Screenshot
Browser Compatibility
* All browser logos displayed above are property of their respective owners.
Downloads