如何从 ASP.NET Core IIS上传大文件一些配置

发布于:2024-12-21 ⋅ 阅读:(177) ⋅ 点赞:(0)

        使用ASP.NET Core上传文件,可以参考官方文档: 使用缓冲模型绑定上传小文件到物理存储

 
        默认情况下,Windows IIS 将maxRequestLengthmaxAllowedContentLength分别限制为 4096 KB 和 30,000,000 字节。要上传大于这些限制的文件,您需要覆盖网站根web.config文件中的默认设置并修改 ASP.NET Core 表单设置。
 
下面是一个如何修改Program.csweb.config文件以增加最大文件上传大小的示例:

Program.cs 

// using packages.
// ...
using Microsoft.AspNetCore.Http.Features;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// ...

builder.Services.Configure<IISServerOptions>(options=>
{
    // 1024MB
    options.MaxRequestBodySize = 104857600;
});

builder.Services.Configure<FormOptions>(options =>
{
    // 1024MB
    options.MultipartBodyLengthLimit = 104857600;
});

var app = builder.Build();

// ...

web.config 

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
        <!-- change the max to 1024 MB -->
        <httpRuntime maxRequestLength="104857600" />
    </system.web>
    <system.webServer>
        <security>
            <requestFiltering>
                <!-- change the max to 1024 MB -->
                <requestLimits maxAllowedContentLength="104857600" />
            </requestFiltering>
        </security>
    </system.webServer>
</configuration>

如果您的应用程序也使用Kestrel设置,您还应该像这样覆盖Program.cs文件中的默认设置: 

// using packages.
// ...

var builder = WebApplication.CreateBuilder(args);

builder.Host.ConfigureWebHostDefaults(webBuilder =>
{
    webBuilder.ConfigureKestrel((context, options) =>
    {
        options.Limits.MaxRequestBodySize = 104857600;
    });
});

// Add services to the container.
// ...

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。 


网站公告

今日签到

点亮在社区的每一天
去签到