如果你已经滚动到这里,那说明你已经尝试了以上的解决方案。如果你正在使用最新的NET CORE版本(5.., 6..)并且使用IIS进行托管,请按照以下步骤操作。
Add the web.config
file to your project and then add the following code there:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<security>
<requestFiltering>
<!-- Handle requests up to 1 GB -->
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
</system.webServer>
</configuration>
Set up the Form Options and IIS Server Options in your Startup.cs
file like this:
services.Configure<IISServerOptions>(options =>
{
options.MaxRequestBodySize = int.MaxValue;
});
services.Configure<FormOptions>(o =>
{
o.ValueLengthLimit = int.MaxValue;
o.MultipartBodyLengthLimit = int.MaxValue;
o.MultipartBoundaryLengthLimit = int.MaxValue;
o.MultipartHeadersCountLimit = int.MaxValue;
o.MultipartHeadersLengthLimit = int.MaxValue;
o.BufferBodyLengthLimit = int.MaxValue;
o.BufferBody = true;
o.ValueCountLimit = int.MaxValue;
});
我试图上传一个大文件,但不知何故该文件未到达控制器操作方法,包括文件参数在内的所有参数仍然是null,就像这样:
[HttpPost]
public async Task<IActionResult> ImportMedicalFFSFile(
Guid operationProgressID,
IFormFile file, // <= getting null here
DateTime lastModifiedDate)
{
...
}
[DisableRequestSizeLimit]
属性:[DisableRequestSizeLimit]
public class ImportedFileController : BaseController
{
...
}
更多信息请参见:
var myLargeString = "this is a large string that I want to send to the server";
$.ajax({
type: "POST",
url: "/MyController/MyAction",
contentType: "application/json",
data: JSON.stringify({ largeString: myLargeString }),
processData: false,
success: function (data) {
console.log("Data received from the server: " + data);
},
error: function (xhr, status, error) {
console.log("Error: " + error);
}
});
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(options =>
{
long mb = 1048576;
options.Limits.MaxRequestBodySize = 150 * mb;
});
var app = builder.Build();
app.Run();