[英]Increase upload file size in Asp.Net core v3.1
我正在嘗試在我的 .NET Core v3.1 Blazor 應用程序中上傳多個文件,但無法超過 30MB 的限制。
搜索這個我發現在 Asp.Net 核心中增加上傳文件大小並嘗試了這些建議,但它不起作用。
所有找到的解決方案都涉及更改 web.config,但我沒有該文件。
此外,我的應用程序在開發期間在 Visual Studio 2019 中運行,但也將作為 WebApp 在 Azure 上運行。
這些是我的設置:
程序.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>().ConfigureKestrel((context, options) =>
{
options.Limits.MaxRequestBodySize = null;
});
});
上傳控制器.cs
[Authorize]
[DisableRequestSizeLimit]
public class UploadController : BaseApiController
在 Startup.cs 中配置服務
services.AddSignalR(e => e.MaximumReceiveMessageSize = 102400000)
.AddAzureSignalR(Configuration["Azure:SignalR:ConnectionString"]);
services.Configure<FormOptions>(options =>
{
options.ValueLengthLimit = int.MaxValue;
options.MultipartBodyLengthLimit = long.MaxValue; // <-- !!! long.MaxValue
options.MultipartBoundaryLengthLimit = int.MaxValue;
options.MultipartHeadersCountLimit = int.MaxValue;
options.MultipartHeadersLengthLimit = int.MaxValue;
});
services.Configure<IISServerOptions>(options =>
{
options.MaxRequestBodySize = int.MaxValue;
});
在 Startup.cs 中配置
app.Use(async (context, next) =>
{
context.Features.Get<IHttpMaxRequestBodySizeFeature>()
.MaxRequestBodySize = null;
await next.Invoke();
});
我錯過了一個設置嗎? 不敢相信這需要這么難。
在https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-3.1找到了解決方案。 最小化的方案只是新增了一個web.config文件,內容如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="52428800" />
</requestFiltering>
</security>
</system.webServer>
</configuration>
似乎還有一些其他設置,例如限制每個操作方法。 您可能需要對它們進行 go 並選擇最適合您需要的任何東西。
ps 今天早些時候在其他地方看到了相同的 web.config 解決方案。 嘗試將 30M 作為 maxAllowedContentLength,但它不適用於約 10MB 的文本文件。 現在實現的請求大小增加了兩倍,因為文件內容作為二進制數組的字符串表示形式發送(這是一個問題,應該處理)。 檢查網絡選項卡以獲取確切的請求大小,並確保它不超過上述 web.config 設置。
使用[DisableRequestSizeLimit]
屬性修飾 controller 方法。
[HttpPost]
[DisableRequestSizeLimit]
[Route("~/upload")]
public async Task<IActionResult> Upload(...)
{
return Ok(...);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.