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(); } |
@{
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> } |
[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(); } |
Now we will press F5 and check the output.
Now check the wwwroot folder for uploaded file.
Download
0 comments:
Please let me know your view