Uploading a file larger than 100 MB with .NET Core 3.1 will result in 400 (Bad Request)

Viewed 1297

I am creating a file upload with .NET Core 3.1.

When I uploaded a file larger than 100 MB, I got an error:

413 Request Entity Too Large

As a result of various investigations, I was able to avoid the 413 error by making the following settings:

web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <security>
        <requestFiltering>
          <!-- Handle requests up to 1 GB -->
          <requestLimits maxAllowedContentLength="1073741824" />
        </requestFiltering>
      </security>
    </system.webServer>
  </location>
</configuration>

UploadController.cs:

        [Route("embed")]
        [HttpPost]
        [DisableRequestSizeLimit]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status400BadRequest)]
        public ActionResult PostEmbed([FromForm] WebPptVoiceEmbedRequest request)
        {
            Log.NLog.Debug("[Get]:PostEmbed start");

            byte[] array;
            using (var stream = new MemoryStream())
            {
                request.File.CopyTo(stream);
                array = stream.ToArray();
            }

            Log.NLog.Debug("[Get]:PostEmbed end");
            
            return File(array, "application/vnd.openxmlformats-officedocument.presentationml.presentation", request.File.FileName);
        }

Program.cs:

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.ConfigureKestrel(options =>
                    {
                        options.Limits.MaxRequestBodySize = null;
                    })
                    .UseStartup<Startup>();
                })
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    logging.SetMinimumLevel(LogLevel.Debug);
                })
                .UseNLog();

client.js

  let embedRequest = new FormData();
  embedRequest.append('file', file);

    const { data } = await axios
      .post(Api.embed, embedRequest, {
        responseType: 'blob',
        dataType: 'binary',
      })
      .catch((error) => {
        apiErrorLogAndThrow(error);
      });
    const blob = new Blob([data]);
    return blob;

However, I still get an error

400 (Bad Request

Is there a place to set the file upload capacity other than the place I set?
It's confusing because there are too many size settings in various places.

I want to solve it first,
If possible, I would like to know how to solve it without using web.config.

Best regards

1 Answers

There is a limit on the size of files that the multipart form body allows for upload.

In UploadController.cs you need to change that limit as well in order to make it work:

Something like:

[HttpPost]
[RequestFormLimits(MultipartBodyLengthLimit = 1073741824)]
Related