Asp.net Core 2.0 RequestSizeLimit属性不起作用 [英] Asp.net Core 2.0 RequestSizeLimit attribute not working
问题描述
我正在Asp.net core 2.0中创建一个网站,该网站允许上传文件.我很快遇到了30MB
上传限制的问题,并从服务器收到了404
响应.低于此限制,一切正常.
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.
我在网络上找到了许多这样的解决方案:
增加Asp.Net core中的上传文件大小.我的问题是我无法使用此解决方案,并且在30MB
上方时仍会收到404
响应.
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
.
我的电话是一个ajax电话,它是这样的:
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!!!
推荐答案
对于其他有相同问题的人,这就是答案.
For anyone else with the same problem, this is the answer.
马克·拉弗勒(Mark LaFleur)的答案是正确的方向,但web.config缺少关键部分.
Mark LaFleur's answer was the right direction but the web.config was missing a crucial section.
我得到了帮助,但是此网页解释了有关Asp.net Core中的web.config文件的更多信息: ASP.NET核心模块配置参考
I was helped but this webpage that explains more about the web.config file in Asp.net Core: ASP.NET Core Module configuration reference
要停止此错误,您需要创建一个具有以下内容的web.config文件:
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>
这篇关于Asp.net Core 2.0 RequestSizeLimit属性不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!