Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

javascript - 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. 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!!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...