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

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

问题描述

当前,我正在使用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.

KestrelServerLimits.cs的Github

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

如果要更改特定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 Core中增加上传文件的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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