ColinChang.BigFileForm 1.1.1

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

Install-Package ColinChang.BigFileForm -Version 1.1.1
dotnet add package ColinChang.BigFileForm --version 1.1.1
<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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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.

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.

Release Notes

move dependent abstractions to ColinChang.BigFileForm.Abstractions

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 194 9/12/2020
1.1.0 76 9/12/2020
1.0.2 57 8/26/2020
1.0.1 69 8/26/2020
1.0.0 72 8/26/2020