Monday, 17 January 2022

Upload / Save File in User Created Folder in Asp.Net Core 6/MVC in C#.Net

1/17/2022 - By Pranav Singh 0

Upload File in User Created Folder in Asp.Net Core 6/MVC in C#.Net

In this article i will show you how you can upload a file in an user created folder which is not present in wwwroot folder in asp.net core 6/ mvc application using c#.net. Now for this article first we will create a new folder which is not present in your wwwroot folder. This uploaded file will not be accessible on http. It will be same as we upload file in App_Data folder. 

Other Related Articles: 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 6Upload File To wwwroot Folder in ASP.NET Core Using C#.Net How To Upload File Asp.net core 6.

Now for this article first we will create new asp.net core 6 / mvc project and add a folder in your project.


upload file outside the wwwroot folder  in asp.net core 6


Now we will add controller and create a view. In this view add the below code. 

@{

    ViewData["Title"] = "Home Page";

}

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @enctype = "multipart/form-data" }))

{

    <span style="font-weight:bold;">Upload File</span>

    <br />

    <span style="color:red;">@ViewBag.Message</span>

    <br />

    <input type="file" name="formFile" />

    <input type="submit" value="Submit" />

}


In above code just check the highlighted part of the code. In this the name of the file control. You need the make sure that the parameter which you pass on postback will be same as the control name. Second thing the enctyp must be added in the form tag.


Now in our controller we will write code to upload the file. For this we will prepare a post method and in this post method we will add the below code.

[HttpPost]

        public IActionResult Index(IFormFile formFile)

        {

            try

            {

                string fileName = Path.GetFileName(formFile.FileName);

                string uploadpath = Path.Combine(Directory.GetCurrentDirectory(), "UserFiles", fileName);

                var stream = new FileStream(uploadpath, FileMode.Create);

                formFile.CopyToAsync(stream);

                ViewBag.Message = "File uploaded successfully.";

            }

            catch

            {

                ViewBag.Message = "Error while uploading the files.";

            }

            return View();

        }


In above code check the highlighted part of the code. The parameter name is same as file input control name.  Now we have done run the code and check the output.


File Upload in Folder (Folder Outside wwwroot) Asp.Net Core 6 / MVC Using C#

Now select the file.

File Upload in Folder (Folder Outside wwwroot) Asp.Net Core 6 / MVC Using C#

Now here is the selected file in input control.

File Upload in Folder (Folder Outside wwwroot) Asp.Net Core 6 / MVC Using C#

Now click on submit button and we will check the received file at controller end.

File Upload in Folder (Folder Outside wwwroot) Asp.Net Core 6 / MVC Using C#


In above code the selected file is visible. Now press F5 and check the output.

File Upload in Folder (Folder Outside wwwroot) Asp.Net Core 6 / MVC Using C#

Now we got the success message and now lets check the specified folder where we have upload the file.


In above code the uploaded file is present.

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