IFormFile - attribute for max file size in megabytes

asp.net-core, asp.net-mvc

Solution

You did not specify the Framework.

ASP.NET:

You should specify the following in web.config:

<configuration>
  <system.web>
    <httpRuntime maxRequestLength="xxx" />
  </system.web>
</configuration>

ASP.NET Core:

IIS

write this code to your web.config ( this file is generated during publish)

       <security>
        <requestFiltering>
          <!-- This will handle requests up to 10MB -->
          <requestLimits maxAllowedContentLength="10485760" />
        </requestFiltering>
      </security>

Kestrel

1, MVC solution:

[RequestSizeLimit(10485760)] 

in the ctrl => this is 10MB

2, Globally specify the IWebHostBuilder

  .UseKestrel(options =>
    {
        options.Limits.MaxRequestBodySize = 10485760; //10MB
    });

Problem

For IFormFile we have attribute: ``` [FileExtensions(Extensions ="jpg,png,gif,jpeg,bmp,svg")] ``` to check extension. Is there any attribute to check file size in Megabytes or I have to write my own attribute? Because I would like to allow users for uploading files with max size = 2 Megabytes.

Original source