在 Asp.Net 核心中增加上传文件的大小 [英] Increase upload file size in Asp.Net core

查看:14
本文介绍了在 Asp.Net 核心中增加上传文件的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在使用 Asp.Net Core 和 MVC6 需要上传文件大小不受限制.我已经搜索了它的解决方案,但仍然没有得到实际的答案.

Currently, I am working with Asp.Net Core and MVC6 need to upload file size unlimited. I have searched its solution but still not getting the actual answer.

我试过这个链接

如果有人有任何想法,请帮忙.

If anyone have any idea please help.

谢谢.

推荐答案

其他答案解决IIS限制.但是,从 ASP.NET Core 2.0 开始,Kestrel 服务器也施加了自己的默认限制.

The other answers solve the IIS restriction. However, as of ASP.NET Core 2.0, Kestrel server also imposes its own default limits.

Github of KestrelServerLimits.cs

关于请求正文大小限制和解决方案的公告(引用如下)

如果要更改特定 MVC 操作或控制器的最大请求正文大小限制,可以使用 RequestSizeLimit 属性.以下将允许 MyAction 接受最多 100,000,000 字节的请求正文.

If you want to change the max request body size limit for a specific MVC action or controller, you can use the RequestSizeLimit attribute. The following would allow MyAction to accept request bodies up to 100,000,000 bytes.

[HttpPost]
[RequestSizeLimit(100_000_000)]
public IActionResult MyAction([FromBody] MyViewModel data)
{

[DisableRequestSizeLimit] 可用于使请求大小不受限制.这有效地恢复了仅属性操作或控制器的 2.0.0 之前的行为.

[DisableRequestSizeLimit] can be used to make request size unlimited. This effectively restores pre-2.0.0 behavior for just the attributed action or controller.

如果请求未由 MVC 操作处理,则仍可以使用 IHttpMaxRequestBodySizeFeature 在每个请求的基础上修改限制.例如:

If the request is not being handled by an MVC action, the limit can still be modified on a per request basis using the IHttpMaxRequestBodySizeFeature. For example:

app.Run(async context =>
{
    context.Features.Get<IHttpMaxRequestBodySizeFeature>().MaxRequestBodySize = 100_000_000;

MaxRequestBodySize 是一个可为空的 long.将其设置为 null 会禁用类似 MVC 的 [DisableRequestSizeLimit] 的限制.

MaxRequestBodySize is a nullable long. Setting it to null disables the limit like MVC's [DisableRequestSizeLimit].

您只能在应用程序尚未开始读取时配置请求的限制;否则抛出异常.有一个 IsReadOnly 属性告诉您 MaxRequestBodySize 属性是否处于只读状态,这意味着配置限制为时已晚.

You can only configure the limit on a request if the application hasn’t started reading yet; otherwise an exception is thrown. There’s an IsReadOnly property that tells you if the MaxRequestBodySize property is in read-only state, meaning it’s too late to configure the limit.

如果要全局修改最大请求正文大小,可以通过修改 UseKestrelUseHttpSys<回调中的 MaxRequestBodySize 属性来完成/代码>.MaxRequestBodySize 在这两种情况下都是可以为空的 long.例如:

If you want to modify the max request body size globally, this can be done by modifying a MaxRequestBodySize property in the callback of either UseKestrel or UseHttpSys. MaxRequestBodySize is a nullable long in both cases. For example:

.UseKestrel(options =>
{
    options.Limits.MaxRequestBodySize = null;

.UseHttpSys(options =>
{
    options.MaxRequestBodySize = 100_000_000;

这篇关于在 Asp.Net 核心中增加上传文件的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆