22

I'm creating a website in Asp.net core 2.0 which allows files to be uploaded. I quickly came across the problem of the 30MB upload limit and receive a 404 response from the server. Below this limit everything works fine.

I found a number of solutions on the web like this one: Increase upload file size in Asp.Net core. My problem is that I cannot get this solution to work and I'm still getting the 404 response when over 30MB.

My call is an ajax one and goes like this:

function uploadMedia(isPhoto, files) {
  var type;
  if (isPhoto) {
    type = "i";
  } else {
    type = "v";
  }

  var data = new FormData();
  if (files.length > 0) {
    for (idx = 0; idx < files.length; idx++) {
      if (files[idx].size < 1074790400) {
        data.append("fileImage" + idx, files[idx]);
      } else {
        BootstrapDialog.show({
          type: BootstrapDialog.TYPE_WARNING,
          title: "Validation Error",
          message: "The maximum file size for images is 1GB. Please resize your image and upload again.",
          buttons: [
            {
              label: "OK",
              action: function(dialogItself) {
                dialogItself.close();
              }
            }
          ]
        });
      }
    }

    $.ajax({
      url: "/api/article/uploadfile/" + type,
      type: "POST",
      processData: false,
      contentType: false,
      dataType: false,
      data: data,
      success: function(jsonData) {
        refreshUploadedImages(jsonData, isPhoto);
      }
    });
  }
}

function rotateImageAnticlockwise(element) {
  var id = $(element).attr("data-id");
  var mediaData = getMediaData(id, true);

  $.ajax({
    url: "/api/article/rotateMedia/a/p/" + mediaData.fileId + "/" + mediaData.rotation,
    type: "POST",
    processData: false,
    contentType: false,
    success: function(jsonData) {
      refreshRotatedImage(jsonData);
    }
  });
}

Then my server-side method has attributes like this:

[HttpPost]
[RequestSizeLimit(1074790400)]
[Route("api/article/uploadfile/{mediaType}")]
public async Task<IActionResult> UploadFile(string mediaType)

Can anyone see what I'm doing wrong? This is driving me mad!!!

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
Slade
  • 2,311
  • 3
  • 21
  • 25
  • 1
    Just a thought, as a troubleshooting step, have you tried 1) commenting out your JS `if (files[idx].size < 1074790400)` block (just the surrounding block so the `data.append` executes and 2) using `[DisableRequestSizeLimit]` on the action method (instead of setting an explicit limit)? – egnomerator May 07 '18 at 19:16
  • @egnomerator - I tried both but still get the same error unfortunately. – Slade May 07 '18 at 19:22
  • Just to be sure I'm not making incorrect assumptions, your app is hosted by IIS and not self-hosted? Sorry if this was specified somewhere and I missed it. – egnomerator May 07 '18 at 19:44
  • I am running the website from Visual Studio as a standard .net core 2.0 asp.net app. – Slade May 07 '18 at 20:02

2 Answers2

26

For anyone else with the same problem, this is the answer.

Mark LaFleur's answer was the right direction but the web.config was missing a crucial section.

I was helped but this webpage that explains more about the web.config file in Asp.net Core: ASP.NET Core Module configuration reference

To stop this error you need to create a web.config file with the following content:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <security>
      <requestFiltering>
        <!-- This will handle requests up to 50MB -->
        <requestLimits maxAllowedContentLength="52428800" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>
Slade
  • 2,311
  • 3
  • 21
  • 25
  • 8
    Thank you so much, it worked like a charm! Do you know another way to fix the same issue without creating the web.config? I mean from the code itself or if we can do it from the built-in appsettings.json in the ASP CORE project. – Yasser Jarouf Nov 09 '18 at 15:06
8

The RequestSizeLimit attribute allows you to define the maximum request size within your code but it is still limited by maximum request size supported by the server itself.

In this case, I suspect you're receiving this error:

HTTP 404.13 - Not Found
The request filtering module is configured to deny a request that exceeds the request content length.

This error is being triggered by ISS because the call exceeded IIS' maxAllowedContentLength value (the default is 30,000,000). From the documentation:

The following error indicates your file upload exceeds the server's configured maxAllowedContentLength. The default setting is 30000000, which is approximately 28.6MB. The value can be customized by editing the web.config:

<system.webServer>
  <security>
    <requestFiltering>
      <!-- This will handle requests up to 50MB -->
      <requestLimits maxAllowedContentLength="52428800" />
    </requestFiltering>
  </security>
</system.webServer>
Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
  • I did try to add a web.config like this but then I encountered another problem. When I try to run the app from Visual Studio I get the following error: 'The web server request failed with status code 500. Internal server error. Then the full response says 'HTTP Error 500.19 - Internal Server Error - The requested page cannot be accessed because the related configuration data for the page is invalid.' – Slade May 07 '18 at 19:42
  • This could be caused by a number of settings. More often than not it turns out to be a feature in ISS isn't installed on the server. Which version of IIS are you using to host the site? – Marc LaFleur May 07 '18 at 19:55
  • I'm not trying to deploy the app to a server, just trying to run it from Visual Studio – Slade May 07 '18 at 20:04
  • Take a look at the comments at https://stackoverflow.com/a/11961615/105518. It sounds like IIS Express requires overriding it's default config. Another option would be to use a local IIS instance instead of IIS Express. – Marc LaFleur May 08 '18 at 14:26
  • Also, if you're just using Kestel without IIS you may want to take a look at these options: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?tabs=aspnetcore2x&view=aspnetcore-2.1#kestrel-options – Marc LaFleur May 08 '18 at 14:28