ColinChang.BigFileForm 1.0.2

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".

There is a newer version of this package available.
See the version list below for details.
Install-Package ColinChang.BigFileForm -Version 1.0.2
dotnet add package ColinChang.BigFileForm --version 1.0.2
<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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ColinChang.BigFileForm, 1.0.2"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. 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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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.

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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.1.1 261 9/12/2020
1.1.0 137 9/12/2020
1.0.2 118 8/26/2020
1.0.1 147 8/26/2020
1.0.0 128 8/26/2020