XelaNimed
XelaNimed

Reputation: 315

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>();
}

Upvotes: 0

Views: 5071

Answers (3)

user22253960
user22253960

Reputation: 11

Exceeded Maximum File Size in web.config - How Can This Be Fixed?

I have encountered an issue with my web.config file on my web server (E:\\WebServer\\DBBweb\\web.config). I realized that the maximum file size limit was exceeded once again. Here are the steps I took to identify and solve the problem:

  1. I found that the size of the web.config file was 251KB, which is larger than the permitted maximum file size of 250KB.
  2. I opened the Registry Editor (Regedit) as an Administrator.
  3. I navigated to the key: HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\InetStp.
  4. I added a subkey named 'Configuration'.
  5. I added a DWORD value named MaxWebConfigFileSizeInKB and set its value to a number greater than 251KB.
  6. I then navigated to the key: HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\InetStp. This key is identical to the previous key but contains the Wow6432Node in the path.
  7. I repeated steps 4 and 5 for this key.
  8. I stopped and started the application pool.
  9. Lastly, I restarted the server.

This procedure had to be done for both keys because the website is utilizing 32-bit compatibility.

Despite this the team and I continue to advise against adding client services in the web.config. We suggest using an APIHANDLER instead, which functions as an extension of the web.config.

Could anyone provide additional solutions or confirm whether this is the most efficient way to handle the issue of exceeding maximum file size in the web.config? Also, could anyone shed light on the benefits of using an APIHANDLER over adding client services directly in the web.config?

Thank you in advance for your assistance.

Upvotes: 1

Hameed
Hameed

Reputation: 1615

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>

Upvotes: 2

Alper Ebicoglu
Alper Ebicoglu

Reputation: 9644

Try this

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
  //add to beginning of the method... 
  services.Configure<FormOptions>(x =>
  {
      x.ValueLengthLimit = int.MaxValue;
      x.MultipartBodyLengthLimit = int.MaxValue;
      x.MultipartHeadersLengthLimit = int.MaxValue;
  });


}

Upvotes: 0

Related Questions