.Net Core 上传大文件

一顿乱操作中实现的:

1.Porgram.cs 中添加:

 public static IHostBuilder CreateHostBuilder(string[] args) =>
          Host.CreateDefaultBuilder(args)
          .ConfigureWebHostDefaults(webBuilder =>
          {
              webBuilder.ConfigureKestrel((context, options) =>
              {
                 options.Limits.MaxRequestBodySize = 524288000;
              });
              webBuilder.UseStartup<Startup>();
          });//GT---

2.在Startup.cs中添加:

services.Configure<FormOptions>(x =>
            {
                x.ValueLengthLimit = int.MaxValue;
                x.MultipartBodyLengthLimit = long.MaxValue; // In case of multipart
            });//GT---

3.在上传文件接口的上面添加:

[HttpPost]
//[MediaSizeLimit]
//[RequestSizeLimit(1_074_790_400)]
[DisableRequestSizeLimit]
[RequestFormLimits(ValueLengthLimit = int.MaxValue, MultipartBodyLengthLimit = long.MaxValue)]
[Route("UploadFile")]
public async Task<ActionResult<object>> UploadFile(string path,ICollection<IFormFile> files)
{

}//GT---

4.在web.config文件中添加:

<security>
       <requestFiltering>
        <requestLimits maxQueryString="602768" maxAllowedContentLength="2147483648" />
       </requestFiltering>
      </security> //GT---


<system.web>
<httpRuntime maxRequestLength="40960000" appRequestQueueLimit="100" useFullyQualifiedRedirectUrl="true" executionTimeout="1000" />
</system.web>//GT---

注意:可能在本地上无法上传文件,要在服务器上测试。记录一下(2020/12/31)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
.NET Core 中,可以通过以下方式配置上传文件的大小限制: 1. 在 Startup.cs 文件中,找到 ConfigureServices 方法,在方法中添加以下代码: ``` services.Configure<IISServerOptions>(options => { options.MaxRequestBodySize = 104857600; // 100MB }); ``` 其中,MaxRequestBodySize 属性表示上传文件的最大限制,以字节为单位。 2. 如果是使用 Kestrel 服务器,则可以在 Program.cs 文件中的 CreateHostBuilder 方法中添加以下代码: ``` public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseKestrel(options => { options.Limits.MaxRequestBodySize = 104857600; // 100MB }); webBuilder.UseStartup<Startup>(); }); ``` 其中,MaxRequestBodySize 属性同样表示上传文件的最大限制,以字节为单位。 需要注意的是,以上两种方式可以同时存在,但是以 Kestrel 配置为准。另外,在配置文件中也可以进行上传文件大小的配置,例如 appsettings.json 文件中: ``` { "AllowedHosts": "*", "MaxAllowedContentLength": 104857600 // 100MB } ``` 其中,MaxAllowedContentLength 属性同样表示上传文件的最大限制,以字节为单位。这种方式需要在 Startup.cs 文件中的 ConfigureServices 方法中添加以下代码: ``` services.Configure<IISServerOptions>(options => { options.MaxAllowedContentLength = Configuration.GetValue<long>("MaxAllowedContentLength"); }); ``` 需要引入 Microsoft.Extensions.Configuration 包。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值