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

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

问题描述

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

我试过这个链接

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

谢谢.

解决方案

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

Kestrel的Github

公布请求正文大小限制及解决方案(引用如下)

MVC 说明

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

[HttpPost][请求大小限制(100_000_000)]公共 IActionResult MyAction([FromBody] MyViewModel 数据){

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

通用中间件说明

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

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

MaxRequestBodySize 是一个可以为 null 的 long.将其设置为 null 会禁用限制,如 MVC 的 [DisableRequestSizeLimit].

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

全局配置说明

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

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

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

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.

I have tried this link

If anyone have any idea please help.

Thanks.

解决方案

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

Announcement of request body size limit and solution (quoted below)

MVC Instructions

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] can be used to make request size unlimited. This effectively restores pre-2.0.0 behavior for just the attributed action or controller.

Generic Middleware Instructions

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 is a nullable long. Setting it to null disables the limit like MVC's [DisableRequestSizeLimit].

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.

Global Config Instructions

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;

or

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

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

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