简体   繁体   English

Asp.net Core 2.0 RequestSizeLimit属性不起作用

[英]Asp.net Core 2.0 RequestSizeLimit attribute not working

I'm creating a website in Asp.net core 2.0 which allows files to be uploaded. 我正在Asp.net core 2.0中创建一个允许上传文件的网站。 I quickly came across the problem of the 30MB upload limit and receive a 404 response from the server. 我很快就遇到了30MB上传限制的问题,并从服务器收到了404响应。 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 . 我在网上找到了许多像这样的解决方案: 增加Asp.Net核心中的上传文件大小 My problem is that I cannot get this solution to work and I'm still getting the 404 response when over 30MB . 我的问题是我无法使这个解决方案工作,而且当超过30MB时我仍然得到404响应。

My call is an ajax one and goes like this: 我的电话是ajax,就像这样:

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!!! 这让我很生气!!!

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. Mark LaFleur的答案是正确的方向,但web.config缺少一个关键部分。

I was helped but this webpage that explains more about the web.config file in Asp.net Core: ASP.NET Core Module configuration reference 我得到了帮助,但这个网页解释了更多关于Asp.net Core中的web.config文件: ASP.NET核心模块配置参考

To stop this error you need to create a web.config file with the following content: 要停止此错误,您需要创建一个包含以下内容的web.config文件:

<?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>

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. RequestSizeLimit属性允许您在代码中定义最大请求大小,但它仍受服务器本身支持的最大请求大小的限制。

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 ). ISS触发此错误,因为调用超出了maxAllowedContentLength值(默认值为30,000,000 )。 From the documentation : 文档

The following error indicates your file upload exceeds the server's configured maxAllowedContentLength . 以下错误表示您的文件上载超出了服务器配置的maxAllowedContentLength The default setting is 30000000 , which is approximately 28.6MB . 默认设置为30000000 ,大约为28.6MB The value can be customized by editing the web.config : 可以通过编辑web.config定义该值:

 <system.webServer> <security> <requestFiltering> <!-- This will handle requests up to 50MB --> <requestLimits maxAllowedContentLength="52428800" /> </requestFiltering> </security> </system.webServer> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM