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 Core MVC (.Net Core).
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.
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 IFormFile class is passed as a parameter.
Note: The name of the IFormFile parameter and the name of HTML FileUpload element must be exact same, otherwise the IFormFile parameter will be NULL.
Then, a check is performed whether Directory (Folder) exists if not then the Directory (Folder) is created using IWebHostEnvironment interface.
An object of FileStream class is created and using CopyTo method of IFormFile class and then the file is saved to the Directory (Folder).
Finally, a message is set in the
ViewBag object and simply the View is returned.
public class HomeController : Controller
{
private IWebHostEnvironment Environment;
public HomeController(IWebHostEnvironment _environment)
{
this.Environment = _environment;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(IFormFile postedFile)
{
string path = Path.Combine(this.Environment.WebRootPath, "Uploads");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string fileName = Path.GetFileName(postedFile.FileName);
using (FileStream stream = new FileStream(Path.Combine(path, fileName), FileMode.Create))
{
postedFile.CopyTo(stream);
ViewBag.Message = "File uploaded successfully.";
}
return View();
}
}
View
HTML Markup
The View consists of HTML Form which has been created using the Html.BeginForm method with the following parameters.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
Attribute – 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>
<form method="post" asp-controller="Home" asp-action="Index" enctype="multipart/form-data">
<input type="file" name="postedFile" multiple="multiple" onchange="UploadFile(this)" />
<br />
<input type="submit" id="btnUpload" value="Upload" style="display:none" />
<br />
<span style="color:green">@Html.Raw(ViewBag.Message)</span>
</form>
<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