ColinChang.BigFileForm 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package ColinChang.BigFileForm --version 1.0.2
NuGet\Install-Package ColinChang.BigFileForm -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ColinChang.BigFileForm" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ColinChang.BigFileForm --version 1.0.2
#r "nuget: ColinChang.BigFileForm, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ColinChang.BigFileForm as a Cake Addin
#addin nuget:?package=ColinChang.BigFileForm&version=1.0.2

// Install ColinChang.BigFileForm as a Cake Tool
#tool nuget:?package=ColinChang.BigFileForm&version=1.0.2

What this is about?

an Asp.Net Core middleware that can process multiple form parameters including big files and texts in a POST/PUT method by overriding object HttpConext.Request.Form and HttpConext.Request.Form.Files.

How to use it?

this middleware is easy to be used by a few steps.

configuration

configures the file size limitation in appsettings.json.

{
  "BigFileFormOptions": {
    "MinBodySize": 5242880,
    "MaxBodySize": 209715200
  }
}

config the options in Startup.ConfigureServices

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<BigFileFormOptions>(Configuration.GetSection(nameof(BigFileFormOptions)));
    services.AddControllers();
}

use the middleware

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseRouting();

    //use big file form middleware
    app.UseBigFileForm();
    app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}

try it

after using this middleware, you could get your big file and text parameters by Request.Form.Files and Request.Forms when you try to upload a big file between MinBodySize and MaxBodySize. It can only work in a POST or PUT method.

[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
    private ILogger _logger;
    public TestController(ILogger<TestController> logger) => _logger = logger;

    [HttpPost]
    [DisableFormValueModelBinding]
    [DisableRequestSizeLimit]
    public async Task PostAsync()
    {
        var releaseNotes = Request.Form["releasenotes"];
        _logger.LogInformation(releaseNotes);

        var app = Request.Form.Files["app"];
        await using var fileStream = System.IO.File.Create(app.FileName);
        await app.CopyToAsync(fileStream);
    }
}

upload big file with multiple parameters

file size limitation

when we try to upload a big file, we have to know both the Kestrel server and default form have its limitation. We could adjust them by configuring KestrelServerOptions and FormOptions.

{
  "KestrelServerOptions": {
    "Limits": {
      "KeepAliveTimeout": 300,
      "RequestHeadersTimeout": 300,
      "MaxRequestBodySize": 209715200,
      "Http2": {
        "MaxStreamsPerConnection": 104857600,
        "MaxFrameSize": 16777215
      }
    }
  },
  "BigFileFormOptions": {
    "MinBodySize": 5242880,
    "MaxBodySize": 209715200
  }
}
public void ConfigureServices(IServiceCollection services)
{
    services
        // modify kestrel limitation
        .Configure<KestrelServerOptions>(Configuration.GetSection(nameof(KestrelServerOptions)))
        // modify default form limitation
        .Configure<FormOptions>(options =>
        {
            var maxRequestBodySize =
                int.Parse(Configuration["KestrelServerOptions:Limits:MaxRequestBodySize"]);
            options.ValueLengthLimit = maxRequestBodySize;
            options.MultipartBodyLengthLimit = maxRequestBodySize;
        })
        // big file form
        .Configure<BigFileFormOptions>(Configuration.GetSection(nameof(BigFileFormOptions)));

    services.AddControllers();
}

Sample

Sample project shows how to use this middleware.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.1 616 9/12/2020
1.1.0 407 9/12/2020
1.0.2 384 8/26/2020
1.0.1 412 8/26/2020
1.0.0 393 8/26/2020