Friday, 14 January 2022

Asp.Net Core 6 : Rename / Change File or Image Name and Upload in wwwroot Using C#.Net

1/14/2022 - By Pranav Singh 0

In this article i will show you how you can rename uploaded file / Image name and upload or save it into wwwroot folder in your asp.net core 6 (web application/MVC) application.

In my previous articles i have shown Upload File To wwwroot Folder in ASP.NET Core Using C#.Net, Upload Multiple File To wwwroot Folder in ASP.NET Core Using C#.Net , Asp.net Core 6 Multiple File Upload


Now for this article first we will create a new asp.net core 6 application. In this we will create a folder in wwwroot folder.



After this we will add a new controller class file and add the below get method in it.


[HttpGet]

        public IActionResult Index()

        {

            return View();

        }


 After this we will create a view named as index.cshtml and add the below mention code.


@{

    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"/>

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

    </div>

}


In above code you must include @enctype = "multipart/form-data" into your form tag. Other wise on submit you will not get the file detail after post back at your controller end. Keep the form tag name same as you define in your controller parameter. Now we will add the post method. 


[HttpPost]

        public IActionResult Index(IFormFile formFile)

        {

            try

            {

 

                Guid guid = Guid.NewGuid();

                string newfileName = guid.ToString();

                string fileextention = Path.GetExtension(formFile.FileName);

                string fileName = newfileName + fileextention;

                string uploadpath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\images", 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 have create a guid and this guid i have used to replace with the uploaded file name before upload. In this we need to get the extension of the uploaded file and after getting extension we combine it and get the file name. While preparing the upload path we have passed the renamed file name.  Now we have done run the application and check the output.


Now  we will select the file and click on upload button.  After that just the code.


Now we will press F5 and check the output.



Now check the wwwroot folder for uploaded file.



Download

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