So, for this article we will create a new asp.net core 8 mvc application. After creating application, we will add a folder in root of the application.
[HttpGet]
public IActionResult Index()
{
return View();
}
In above code I have define a method with return type as IActionResult with HttpGet method. Now let's create a view. In a view we will add our Html code base. @{
ViewData["Title"] = "Home Page";
}
@using (Html.BeginForm("Index", "UploadFile", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
<div style="color:red" id="divmessage">@ViewBag.Message</div>
<div style="font-weight:bold;">Select File:</div>
<div>
<input type="file" class="form-control" name="file" id="filecontrol" style="width:300px;" />
</div>
<br />
<div>
<input type="submit" value="Upload File" />
</div>
}
Here Html.BeginForm methos is used for defining the form tag. In this method we need to add the Action name and the controller name and Form Method Type as Post. This piece of code will define that when user click on submit button which method need to execute at controller end. Here one of the most important thing which we need to take care new { @enctype = "multipart/form-data" }. If we don't add this html attribute, we will not be able to get the selected file at the controller end.In next line I have taken the div control to display the message. Here I have taken the @ViewBag.Message to display the message send from controller end. In next line I have taken the input control of file type. to upload the browse the file. Here I have taken the file control name as name='file'. This name of the file must be same as the parameter passed to the HttpPost method. In next line I have taken the Submit to submit the file to upload.
Now let's add the HttpPost method. Which will execute when we select the file and click on Submit button.
[HttpPost]
public IActionResult Index(IFormFile file)
{
if (file != null)
{
try
{
string parentPath = "DemoFolder";
string uploadfilepath = Path.Combine(Directory.GetCurrentDirectory(), parentPath, file.FileName);
var filestream = new FileStream(uploadfilepath, FileMode.Create);
file.CopyToAsync(filestream);
string dbSaveUploadPath = string.Format("{0}{1}", @"\Demofiles\", file.FileName);
ViewBag.Message = "File uploaded successfully.";
}
catch (Exception ex)
{
ViewBag.Message = ex.Message;
}
}
else
{
ViewBag.Message = "Please select file to upload.";
}
return View();
}
In above code I have passed a parameter which is of IFormFile, and parameter name is "file". So, one thing we need to notice that the input file name and the passed the name is same. After that I have validated that the file is selected or not. If the file is not selected, we will display the error message to the user. After this I have declared a variable in this I have defined the folder name which is created outside the wwwroot folder. So here we need to take care one thing which we just need to define the folder name. So, it should be defined "DemoFolder" instead of "\Demofiles\".
Now we will prepare the path of the uploaded file Path.Combine(Directory.GetCurrentDirectory(), parentPath, file.FileName). In this we have defined the current directory and the folder name and the file name. After that I have used FileStream to make a copy of uploaded file in the given path. Now we have done let's run the code and check the output.