ColinChang.BigFileForm 1.1.1

dotnet add package ColinChang.BigFileForm --version 1.1.1                
NuGet\Install-Package ColinChang.BigFileForm -Version 1.1.1                
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.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ColinChang.BigFileForm --version 1.1.1                
#r "nuget: ColinChang.BigFileForm, 1.1.1"                
#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.1.1

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

What this is about?

an extension for Asp.Net Core HttpRequest that can process multiple form parameters including big files and texts in a POST/PUT method.

How to use it?

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

configuration

configures the file size limitation in appsettings.json.

{
  "BigFileFormOptions": {
    "FileSizeLimit": 209715200,
    "PermittedExtensions": [
      ".apk",
      ".ipa"
    ]
  }
}

config the options in Startup.ConfigureServices

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

try it

this only works in a POST or PUT method.

[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
    private readonly BigFileFormOptions _options;
    private readonly string _baseDirectory;
    private readonly ILogger _logger;

    public TestController(IOptions<BigFileFormOptions> options, IHostEnvironment env,
        ILogger<TestController> logger)
    {
        _options = options.Value;
        _baseDirectory = env.ContentRootPath;
        _logger = logger;
    }

    [HttpPost]
    [DisableFormValueModelBinding]
    [DisableRequestSizeLimit]
    public async Task PostAsync()
    {
        var parameters = await Request.ExtractFormAsync(_options, (name, fileName) =>
            System.IO.File.Create(Path.Combine(_baseDirectory, WebUtility.HtmlEncode(fileName))));

        string releaseNotes;
        releaseNotes = parameters.Texts[nameof(releaseNotes).ToLower()];
        _logger.LogInformation($"{nameof(releaseNotes)}:{releaseNotes}");

        string app;
        if (parameters.Files.TryGetValue(nameof(app), out app))
            _logger.LogInformation($"{nameof(app)}:{app}");

        foreach (var (key, value) in parameters.Errors)
            _logger.LogError($"error occured when process {key}: {value} ");
    }
}

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": {
      "FileSizeLimit": 209715200,
      "PermittedExtensions": [
        ".apk",
        ".ipa"
      ]
    }
}
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 extension.

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 647 9/12/2020
1.1.0 429 9/12/2020
1.0.2 412 8/26/2020
1.0.1 433 8/26/2020
1.0.0 415 8/26/2020

move dependent abstractions to ColinChang.BigFileForm.Abstractions