Search code examples
c#asp.net-coreasp.net-core-mvcaspnetboilerplate

How to increase the limit on the size of the uploaded file?


I'm use ABP Zero Core MVC template (ASP.Net Core 2.x, MVC, jQuery) version 4.2.0. When I try to upload a file using the AJAX to controller, I get an HTTP 404.13 error, which indicates that the maximum file size is exceeded. Here and here I found solutions to similar problem and try it solved so ajust, but they either do not fit the pattern used, or I'm doing something wrong.

How to increase the maximum upload file size in Zero Core?

// *.Web.Host\Startup\Program.cs 
// *.Web.Mvc\Startup\Program.cs
public static IWebHostBuilder CreateWebHostBuilder(string[] args) {
    return WebHost.CreateDefaultBuilder(args)
        .UseKestrel(options => { 
            // no effect...  I can only small files uploaded
            options.Limits.MaxRequestBodySize = 1024 * 1024 * 1024; 
        })
        .UseStartup<Startup>();
}

Solution

  • Have you tried to decorate the controller action with [RequestSizeLimit(YOUR_MAX_TOTAL_UPLOAD_SIZE)] along with the changes in Startup.cs?

    services.Configure<FormOptions>(opt =>
    {
        opt.MultipartBodyLengthLimit = YOUR_MAX_TOTAL_UPLOAD_SIZE;
    });
    

    btw, if you are planning to host the app on IIS, you can add web.config file to your project and configure the upload size there, rather than configuring it on the IIS server.

    EDIT: as @XelaNimed's comment, adding the web.config file along with editing the startup.cs, got the code working.

    <configuration>
      <location path="." inheritInChildApplications="false">
       <system.webServer>
         <handlers>
           <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
         </handlers>
         <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout">
           <environmentVariables />
         </aspNetCore>
       </system.webServer>
     </location>
     <system.webServer>
       <security>
          <requestFiltering>
            <requestLimits maxAllowedContentLength="YOUR_BYTES" />
           </requestFiltering>
        </security>
      </system.webServer>
    </configuration>