我有一个 Angular 应用程序,我尝试使用 ng2-file-upload 将文件上传到我的服务器,但当文件很大时,我不断收到 413 错误。我预计这是服务器端问题,但我提到了客户端,以防有人以前见过该上传器的此问题。
我的服务器应用程序是一个 .NET7 Web 应用程序,但我正在将 Angular 应用程序编译到应用程序的 wwwroot 目录中,并且我添加了 web.config 以允许我添加重写规则以在同一系统中支持它们。
到目前为止,我已经添加了: 在 web.config 中,
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="50000000" />
</requestFiltering>
</security>
<serverRuntime uploadReadAheadSize="50000000" />
</system.webServer>
<system.web>
<httpRuntime maxRequestLength="50000" />
</system.web>
在我的上传端点上,
[RequestSizeLimit( 50_000_000 )]
// or
[DisableRequestSizeLimit]
在Program.cs中,
var builder = WebApplication.CreateBuilder( args );
/* ..... */
builder.Services
.Configure<FormOptions>( x =>
{
x.ValueLengthLimit = 50000000;
x.MultipartBodyLengthLimit = 50000000; // in case of multipart
x.MultipartHeadersLengthLimit = 50000000;
} )
.Configure<IISServerOptions>( x =>
{
x.MaxRequestBodySize = 50000000;
} );
/* ..... */
var app = builder.Build()
/* ..... */
app.Use( async ( context, next ) =>
{
var feature = context.Features.Get<IHttpMaxRequestBodySizeFeature>();
if( feature != null )
feature.MaxRequestBodySize = 50000000;
await next.Invoke();
} );
关于我所缺少的还有其他想法吗?我需要添加一些内容才能允许 web.config 被获取吗?我是否需要按特定顺序(相对于其他设置)进行 C# 更改才能生效?
现在,我正在尝试在我的开发环境中使其正常工作(IIS Express 在 VS 2022 中运行)。我最终需要它在 Azure 应用服务中工作,如果这对我处理解决方案的方式有任何影响的话。
想法?
您可以尝试在iis中设置uploadReadAheadSize值来解决这个问题: