how to set the file size limit in razor pages?

mc 4,836 Reputation points
2023-05-05T07:50:45.2466667+00:00

I create UploadImage.cshtml and OnPostUploadImageAsync

the Request.Form.Files is exception:

Multipart body length limit 16384 exceeded.

I set the opt:

builder.Services.AddRazorPages(options =>
{
options.Conventions.AddPageApplicationModelConvention("/Manage/ImageUpload", x => { x.Filters.Add(new RequestFormLimitsAttribute() { MultipartBodyLengthLimit = 268435456 }); });

and the

builder.Services.Configure<Microsoft.AspNetCore.Http.Features.FormOptions>(x =>
{

x.ValueLengthLimit = 268435456;
x.MultipartBodyLengthLimit = 268435456;

});

and [RequestFormLimits(MultipartBodyLengthLimit = 268435456)]

but still the exception.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,682 questions
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,646 Reputation points Microsoft Vendor
    2023-05-08T10:07:33.1033333+00:00

    Hi @mc

    I am not hosting iis but hosting dotnet run.

    You are using Kestrel, so you need to configure request body limit on Kestrel, code like this:

    builder.WebHost.ConfigureKestrel(opt =>
    {
        opt.Limits.MaxRequestBodySize=null;  //disable the request body limit.
    });
    

    Then, configure the FormOptions as below:

    builder.Services.Configure<FormOptions>(options =>
    {
        options.ValueLengthLimit = int.MaxValue;
        options.MultipartBodyLengthLimit = int.MaxValue; 
    }); 
    

    In the Razor page Post method, get the upload file from the IFormCollection, instead of using IFormFile and model binding.

        public class FileUploadModel : PageModel
        {
            private IWebHostEnvironment _environment;
            public FileUploadModel(IWebHostEnvironment environment)
            {
                _environment = environment;
            }
     
    
            [RequestFormLimits(MultipartBodyLengthLimit = 268435456)] 
            public async Task OnPostAsync([FromForm] IFormCollection formData)
            {
                var file = formData.Files["file"];
                if (file != null && file.Length > 0)
                {
                    var filePath = Path.Combine(_environment.WebRootPath, "uploads", file.FileName);
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await file.CopyToAsync(stream);
                    }
                }
    
            }
        }
    

    The Razor page as below: without using model binding, just using an input file element.

    @page
    @model RazorWebApp.Pages.FileUploadModel
     
    <form method="post" enctype="multipart/form-data">
        <input id="file" type="file" name="file" />
        <input type="submit" />
    </form>
    

    The result as below: the upload file size 197MB.

    image2


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 68,306 Reputation points
    2023-05-05T15:23:25.3433333+00:00

    If hosting with IIS you must also set its max limit

    <system.webServer>
     <security>
       <requestFiltering>
         <requestLimits maxAllowedContentLength="268435456" />
       </requestFiltering>
     </security>
    </system.webServer>
    
    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.