Search code examples
c#asp.net-corerazorfile-uploadrazor-pages

Cannot upload relatively large video file using Razor Pages - "Request Entitiy Too Large"


I'm trying to create a web app where I can upload video files to the cloud and then play them back. I am having trouble with uploading relative large video files. When I Upload a relatively small video file everything works as it should.

I have done extensive research into this but I cannot seem to get anything to work. I have recreated my problem in a small example for demonstration purposes

Here is the .cshtml:

<form method="post" enctype="multipart/form-data">
    <input type="file" asp-for="UploadedFile" />
    <input asp-page-handler="Upload" class="btn" type="submit" value="Upload">
</form>

And here is the .cshtml.cs

[RequestFormLimits(MultipartBodyLengthLimit = 104857600)]
public class TrialUploadModel : PageModel
{

    [BindProperty]
    public IFormFile UploadedFile { get; set; }
    public void OnGet()
    {
    }


    public async Task<IActionResult> OnPostAsync()
    {

        Console.Write("Success!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        using (var stream = UploadedFile.OpenReadStream())
        {
            Console.Write(stream.Length);
        }
        return Page();

    }
}

As you can see I have added [RequestFormLimits(MultipartBodyLengthLimit = 104857600)] to the page and I have also created a web.config and extend the limit size there as well, but still I keep getting the same problem.

I have also increased the FormOptions in the StartUp.cs but still no luck:

services.Configure<FormOptions>(x =>
{
    x.ValueLengthLimit = int.MaxValue;
    x.MultipartBodyLengthLimit = int.MaxValue;
    x.MultipartHeadersLengthLimit = int.MaxValue;
});

services.Configure<KestrelServerOptions>(options =>
{
    options.Limits.MaxRequestBodySize = int.MaxValue;
});

When I launch the application using IIS I get this:

RequestTooLargeError


Solution

  • For .NET Core 6 and Razor Pages, there are some tips in Microsoft documentation (https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-6.0)

    In Program.cs, file limits are included as an option targeting the page that performs the upload:

    // Add services to the container.
    builder.Services.AddRazorPages(options => 
    {
    options.Conventions
        .AddPageApplicationModelConvention("/[your cshtml page name with no extension]",
            model =>
            {
                model.Filters.Add(
                new RequestFormLimitsAttribute()
                {
                    // Set the limit to 256 MB
                    ValueLengthLimit = 268435456,
                    MultipartBodyLengthLimit = 268435456,
                    MultipartHeadersLengthLimit = 268435456
                });
               // model.Filters.Add(
               //     new RequestSizeLimitAttribute(268435456));
            });
        });
    

    MultipartHeadersLengthLimit was added in the documentation, but I included other limits you would be interested in changing, too.

    In your page model, include:

    [DisableRequestSizeLimit]
    [RequestFormLimits(MultipartBodyLengthLimit = 268435456)]
    public class YourPageNameModel : PageModel
    { ...}
    

    Note that to follow the documentation example, you must update the FileHelper.cs with the proper file signature if you are trying to upload a different extension. Otherwise, the page will return an invalid state.