Upload Very Large File in Asp.net Core MVC Using C#.Net

How to upload a very large file of size 1GB in asp.net core mvc using c#.net. RequestSizeLimit & RequestFormLimits is to configure the file size limit
In today's article I will show a simple tutorial with an example how you can upload a very large file (size more then 1GB) in asp.net core 8 mvc (.NET core 6, .NET core 7, .NET core 3 etc) using c#.net. now we will create a new asp.net core mvc 8 application and add a controller class file. Add Index method annotating it with HttpGet. This will act as a page load in asp.net core mvc application.

[HttpGet]
public IActionResult Index()
{
    return View();
} 
After this we will create a new folder in wwwroot folder. 


Now let's create the view and add the below code in it. In this view we will use Input control of file type and a submit button. We will use ViewBag to display the message.
 @{
    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. 

Select File in Asp.net Core MVC
Now let's create a very large file. which we will try to upload. 

Large File to Upload
Here in above I have taken file of size 250MB. Now let's upload this file and check. 

Selected Large File

Now click on "Upload File" to update the file. 

File Uploaded successfully

Now let's check the wwwroot folder. 

File in wwwroot Folder

This taken less than a second to upload a 250MB file 

Post a Comment