Search code examples
asp.net.netasp.net-coreasp.net-core-mvclarge-file-upload

I created a .NET Core 7 application, for pose detection, Not able to load large files


I am getting the following when Trying to upload a video file larger than 15 seconds for a form,

enter image description here

The video model is as follow,

public class VideoUploadViewModel
{
    [Required]
    [Display(Name = "Video File")]
    public IFormFile VideoFile { get; set;}
    public string VideoFileName { get; set; }
    public string InferenceModelType { get; set; } = "Body25";
    public string OriginalVideoFilePath { get; set; }
    public string OverlayVideoFilePath { get; set; }
}

also,

I am using the form to upload a file, @Html.TextBoxFor(m => m.VideoFile, new {type = "file", @class = "form-control"})

I tried using the following method to increase the limit,

builder.WebHost.ConfigureKestrel(options =>
{
    options.Limits.MaxRequestBodySize = 512 * 1024 * 1024;
});

builder.Services.Configure<FormOptions>(options =>
{
    options.MultipartBodyLengthLimit = 512 * 1024 * 1024;
});

But still I am not able to upload large files.

Cannot figure it out in .NET 7,also there is no web.config created when I create a new project using Visual Studio.


Solution

  • Try the below code in your Program.cs:

    builder.Services.Configure<KestrelServerOptions>(options =>
    {
         options.Limits.MaxRequestBodySize = int.MaxValue; // if don't set default value is: 30 MB
    });
      //For application running on IIS:
     builder.Services.Configure<IISServerOptions>(options =>
      {
      options.MaxRequestBodySize = int.MaxValue;
      });
    builder.Services.Configure<FormOptions>(x =>
    {
         x.ValueLengthLimit = int.MaxValue;
         x.MultipartBodyLengthLimit = int.MaxValue; // if don't set default value is: 128 MB
         x.MultipartHeadersLengthLimit = int.MaxValue;
    });
    

    IIS content length limit

    The default request limit (maxAllowedContentLength) is 30,000,000 bytes, which is approximately 28.6MB. Customize the limit in the web.config file(you can custom create a web.config file):

    <system.webServer>
      <security>
        <requestFiltering>
          <!-- Handle requests up to 1 GB -->
          <requestLimits maxAllowedContentLength="1073741824" />
        </requestFiltering>
      </security>
    </system.webServer>