Now for this we will create a new asp.net core 6 application. Now in wwwroot folder we will add an folder named as images.
Now we will add a controller class file and add the below code in it.
[HttpGet] public IActionResult Index() { return View(); } |
@{
ViewData["Title"] = "Upload Page"; } @using (Html.BeginForm("Index", "Home",
FormMethod.Post, new {
@enctype = "multipart/form-data" })) {
<div class="text-center"> <h1 class="font-weight-bold">Upload
File</h1> <p style="color:red;font-weight:bold;"> @ViewBag.Message </p> <input type="file" name="formFile" multiple/> <input type="submit" value="Upload" />
</div> } |
After creating the view we will add http post core in our controller file.
[HttpPost] public IActionResult Index(List<IFormFile> formFile) { try { foreach (var file in formFile) { string fileName = file.FileName; fileName =
Path.GetFileName(fileName); string uploadpath =
Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\images", fileName); var stream = new FileStream(uploadpath,
FileMode.Create); file.CopyToAsync(stream); } ViewBag.Message = "File uploaded successfully."; } catch { ViewBag.Message = "Error while uploading the files."; }
return View(); } |
In above code i have taken List<IFormFile> formFile as a parameter. So this we have done to get the multiple selected files for upload. Now we have done run the application to check the output.
Now select the multiple file.
AS we click on upload button break point will hit. and at controller end we can see that we are getting app 4 selected files.
Now press F5 and check the image folder present in wwwroot folder.
0 comments:
Please let me know your view