HttpMultipartParser 7.0.0

.NET 5.0 .NET Standard 2.1 .NET Framework 4.8
NuGet\Install-Package HttpMultipartParser -Version 7.0.0
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.
dotnet add package HttpMultipartParser --version 7.0.0
<PackageReference Include="HttpMultipartParser" Version="7.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HttpMultipartParser --version 7.0.0
#r "nuget: HttpMultipartParser, 7.0.0"
#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 HttpMultipartParser as a Cake Addin
#addin nuget:?package=HttpMultipartParser&version=7.0.0

// Install HttpMultipartParser as a Cake Tool
#tool nuget:?package=HttpMultipartParser&version=7.0.0

Http Multipart Parser

License Sourcelink Build status tests Coverage Status CodeFactor

Release Notes NuGet (stable) MyGet (prerelease)
GitHub release Nuget MyGet Pre Release

About

The Http Multipart Parser does it exactly what it claims on the tin: parses multipart/form-data. This particular parser is well suited to parsing large data from streams as it doesn't attempt to read the entire stream at once and procudes a set of streams for file data.

Installation

The easiest way to include HttpMultipartParser in your project is by adding the nuget package to your project:

PM> Install-Package HttpMultipartParser

.NET framework suport

  • The parser was built for and tested on .NET 4.8, .NET standard 2.1, .NET 5.0 and .NET 6.0.
  • Version 5.1.0 was the last version that supported .NET 4.6.1, NET 4.7.2 and .NET standard 2.0.
  • Version 2.2.4 was the last version that supported older .NET platforms such as .NET 4.5 and .NET standard 1.3.

Usage

Non-Streaming (Simple, don't use on very large files)

  1. Parse the stream containing the multipart/form-data by invoking MultipartFormDataParser.Parse (or it's asynchronous counterpart MultipartFormDataParser.ParseAsync).
  2. Access the data through the parser.

Streaming (Handles large files)

  1. Create a new StreamingMultipartFormDataParser with the stream containing the multipart/form-data
  2. Set up the ParameterHandler and FileHandler delegates
  3. Call parser.Run() (or it's asynchronous counterpart parser.RunAsync())
  4. The delegates will be called as data streams in.

Examples

Single file

// stream:
-----------------------------41952539122868
Content-Disposition: form-data; name="username"

example
-----------------------------41952539122868
Content-Disposition: form-data; name="email"

example@data.com
-----------------------------41952539122868
Content-Disposition: form-data; name="files[]"; filename="photo1.jpg"
Content-Type: image/jpeg

ExampleBinaryData012031203
-----------------------------41952539122868--

// ===== Simple Parsing ====
// You can parse synchronously:
var parser = MultipartFormDataParser.Parse(stream);

// Or you can parse asynchronously:
var parser = await MultipartFormDataParser.ParseAsync(stream).ConfigureAwait(false);

// From this point the data is parsed, we can retrieve the
// form data using the GetParameterValue method.
var username = parser.GetParameterValue("username");
var email = parser.GetParameterValue("email")

// Files are stored in a list:
var file = parser.Files.First();
string filename = file.FileName;
Stream data = file.Data;

// ==== Advanced Parsing ====
var parser = new StreamingMultipartFormDataParser(stream);
parser.ParameterHandler += parameter => DoSomethingWithParameter(parameter);
parser.FileHandler += (name, fileName, type, disposition, buffer, bytes, partNumber, additionalProperties) =>
{
    // Write the part of the file we've received to a file stream. (Or do something else)
    filestream.Write(buffer, 0, bytes);
}

// You can parse synchronously:
parser.Run();

// Or you can parse asynchronously:
await parser.RunAsync().ConfigureAwait(false);

Multiple Parameters

// stream:
-----------------------------41952539122868
Content-Disposition: form-data; name="checkbox"

likes_cake
-----------------------------41952539122868
Content-Disposition: form-data; name="checkbox"

likes_cookies
-----------------------------41952539122868--

// ===== Simple Parsing ====
// You can parse synchronously:
var parser = MultipartFormDataParser.Parse(stream);

// Or you can parse asynchronously:
var parser = await MultipartFormDataParser.ParseAsync(stream).ConfigureAwait(false);

// From this point the data is parsed, we can retrieve the
// form data from the GetParameterValues method
var checkboxResponses = parser.GetParameterValues("checkbox");
foreach(var parameter in checkboxResponses)
{
    Console.WriteLine("Parameter {0} is {1}", parameter.Name, parameter.Data)
}

Multiple Files

// stream:
-----------------------------41111539122868
Content-Disposition: form-data; name="files[]"; filename="photo1.jpg"
Content-Type: image/jpeg

MoreBinaryData
-----------------------------41111539122868
Content-Disposition: form-data; name="files[]"; filename="photo2.jpg"
Content-Type: image/jpeg

ImagineLotsOfBinaryData
-----------------------------41111539122868--

// ===== Simple Parsing ====
// You can parse synchronously:
var parser = MultipartFormDataParser.Parse(stream);

// Or you can parse asynchronously:
var parser = await MultipartFormDataParser.ParseAsync(stream).ConfigureAwait(false);

// Loop through all the files
foreach(var file in parser.Files)
{
    Stream data = file.Data;

    // Do stuff with the data.
}

// ==== Advanced Parsing ====
var parser = new StreamingMultipartFormDataParser(stream);
parser.ParameterHandler += parameter => DoSomethingWithParameter(parameter);
parser.FileHandler += (name, fileName, type, disposition, buffer, bytes, partNumber, additionalProperties) =>
{
    // Write the part of the file we've received to a file stream. (Or do something else)
    // Assume that filesreamsByName is a Dictionary<string, FileStream> of all the files
    // we are writing.
    filestreamsByName[name].Write(buffer, 0, bytes);
};
parser.StreamClosedHandler += () 
{
    // Do things when my input stream is closed
};

// You can parse synchronously:
parser.Run();

// Or you can parse asynchronously:
await parser.RunAsync().ConfigureAwait(false);

Licensing

This project is licensed under MIT.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.1
.NET Framework net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (20)

Showing the top 5 NuGet packages that depend on HttpMultipartParser:

Package Downloads
StrongGrid

StrongGrid is a strongly typed .NET client for SendGrid's v3 API.

ZoomNet

ZoomNet is a strongly typed .NET client for Zoom's API.

IctBaden.Stonehenge3

X-Platform Web Application Framework

RapidCMS.Api.Functions

Package for creating a companion Function API which allows you to easily create backend repositories for front-end collections in RapidCMS WebAssembly. RapidCMS is a Blazor framework which allows you to build a responsive and flexible CMS purely from code.

ApiLibs

An easily extendable restclient library for csharp

GitHub repositories (5)

Showing the top 5 popular GitHub repositories that depend on HttpMultipartParser:

Repository Stars
sendgrid/sendgrid-csharp
The Official Twilio SendGrid C#, .NetStandard, .NetCore API Library
uholeschak/ediabaslib
.NET BMW and VAG Ediabas interpreter library
ThomasBleijendaal/RapidCMS
RapidCMS is a Blazor framework which allows you to build a responsive and flexible CMS purely from code. It provides a basic set of editors and controls, and is fully customisable.
Jericho/StrongGrid
Strongly typed library for the entire SendGrid v3 API, including webhooks
yahehe/Nancy.Swagger
Nancy plugin for generated API documentation in Swagger format.
Version Downloads Last updated
7.0.0 27,764 7/7/2022
6.0.1 22,963 5/31/2022
5.1.0 99,708 3/5/2022
5.0.1 660,767 7/11/2021
5.0.0 899,567 1/3/2021
4.4.0 42,107 12/4/2020
4.3.1 306,543 5/1/2020
4.3.0 570 5/1/2020
4.2.0 46,059 3/30/2020
4.1.0 1,428 3/22/2020
4.0.0 32,648 3/11/2020
3.1.0 113,164 12/15/2019
3.0.0 57,931 11/25/2019
2.2.4 750,542 6/16/2017
2.2.3 45,151 1/11/2017
2.2.2 1,300 1/10/2017
2.2.1 215,261 12/7/2016
2.2.0 44,247 12/5/2016
2.1.7 15,183 10/30/2016
2.1.6 17,999 9/23/2016
2.1.5 1,360 9/23/2016
2.1.4 41,316 4/18/2016
2.1.3 4,967 2/13/2016
2.1.2 1,339 2/13/2016
2.1.1 4,835 1/3/2016
2.0.1 4,352 10/18/2015
2.0.0 4,567 8/23/2015
1.1.5 3,119 7/7/2015
1.1.4 18,666 2/13/2015
1.1.3 5,588 2/13/2015
1.1.2 1,454 2/6/2015
1.1.0 1,424 2/6/2015
1.0.3 1,422 2/6/2015
1.0.2 2,473 11/25/2014