[HttpGet]
public IActionResult Index()
{
return View();
}
After this we will create a new folder in wwwroot folder.
@{
ViewData["Title"] = "UploadFile 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" />
</div>
<br />
<div>
<input type="submit" value="Upload File" />
</div>
}
In above code 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. I have used Input control of type of file. Here one thing we need to the name of control is name="file" in the HttpPost method we will keep the parameter name same as the file upload control name. In last I have taken the input button of Submit type. Now let's check the HttpPost method.
[HttpPost]
[RequestSizeLimit(10L * 1024L * 1024L * 1024L)]
[RequestFormLimits(MultipartBodyLengthLimit = 10L * 1024L * 1024L * 1024L)]
public IActionResult Index(IFormFile file)
{
if (file != null)
{
try
{
string parentPath = @"wwwroot\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}", @"\Demofolder\", file.FileName);
ViewBag.Message = "File uploaded successfully.";
}
catch (Exception ex)
{
ViewBag.Message = ex.Message;
}
}
else
{
ViewBag.Message = "Please select file to upload.";
}
return View();
}
Here after HttpPost i have defined the [RequestSizeLimit(10L * 1024L * 1024L * 1024L)] and [RequestFormLimits(MultipartBodyLengthLimit = 10L * 1024L * 1024L * 1024L)]. Here the max upload file size is 10GB.
This is to set the limit of the uploaded file. This will help us to define the max size of the uploaded file.
In above code I have taken the IFormFile as a parameter and named as file. So, in above statement I said the input file control I have taken the name of control as name="file" and the parameter name as "file". Now I have validated file have been selected or not. If file is selected on that case, we proceed. In next line I have declared the variable named as parentPath. In this I have declared the path of the folder where we need to upload the file. With the help of Path.Combine to concatenate the path the file. With the help of FileName i have used to get the file name. Now I have used FileStream to create the file. In this I have used CopyToAsync to copy the FileStream into the folder.
Now click on "Upload File" to update the file.
Now let's check the wwwroot folder.
This taken less than a second to upload a 250MB file