Tuesday, 4 January 2022

Upload Multiple File To wwwroot Folder in ASP.NET Core Using C#.Net | Asp.net Core 6 Multiple File Upload | How To Upload Multiple File Asp.net core 6

1/04/2022 - By Pranav Singh 0

Upload Multiple File To wwwroot Folder in ASP.NET Core Using C#.NetIn this article i will show you how you can upload multiple files to wwwroot folder (image folder or any folder) in asp.net core 6 using C#.net. I have already shown to you how you can upload a file in wwwroot folder using asp.net core 6 using c#.net

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. 

Upload Multiple File To wwwroot Folder in ASP.NET Core Using C#.Net

Now we will add a controller class file and add the below code in it.


[HttpGet]

        public IActionResult Index()

        {

            return View();

        }

 

After this we will create a view and add the below code in it. 


@{

    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>

}

 

In above code check the highlighted part of the code. In above code i have added "@enctype = "multipart/form-data"  " You must add this without this you will not be able to get the upload file detail at controller end. 


Now point no 2 multiple add this tag in file upload control to make the control enable to select multiple files.


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.

Upload Multiple File To wwwroot Folder in ASP.NET Core Using C#.Net

Now select the multiple file.

Upload Multiple File To wwwroot Folder in ASP.NET Core Using C#.Net


Here you can see file upload control showing 4 files. Now click on upload button.


Upload Multiple File To wwwroot Folder in ASP.NET Core Using C#.Net

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.

Upload Multiple File To wwwroot Folder in ASP.NET Core Using C#.Net


Now press F5 and check the image folder present in wwwroot folder.

Upload Multiple File To wwwroot Folder in ASP.NET Core Using C#.Net

Here are the uploaded files.

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top