10

I am just trying to write a multipart parser but things getting complicated and want to ask if anyone knows of a ready parser in C#!

Just to make clear, I am writing my own "tiny" http server and need to pars multipart form-data too!

Thanks in advance, Gohlool

Gohlool
  • 363
  • 2
  • 5
  • 15
  • Well, I am parsing a multipart/form-data stream received from browser by uploading a file and some fields! – Gohlool Oct 07 '10 at 11:38
  • Yes, there is : http://stackoverflow.com/questions/7460088/reading-file-input-from-a-multipart-form-data-post/21689347#21689347 (even though it has ASP.NET in the name you can use it anywhere, specifically you don't need to be running under ASP.NET) – Ohad Schneider Feb 10 '14 at 22:42

5 Answers5

27

I open-sourced a C# Http form parser here.

This is slightly more flexible than the other one mentioned which is on CodePlex, since you can use it for both Multipart and non-Multipart form-data, and also it gives you other form parameters formatted in a Dictionary object.

This can be used as follows:

non-multipart

public void Login(Stream stream)
{
    string username = null;
    string password = null;

    HttpContentParser parser = new HttpContentParser(stream);
    if (parser.Success)
    {
        username = HttpUtility.UrlDecode(parser.Parameters["username"]);
        password = HttpUtility.UrlDecode(parser.Parameters["password"]);
    }
}

multipart

public void Upload(Stream stream)
{
    HttpMultipartParser parser = new HttpMultipartParser(stream, "image");

    if (parser.Success)
    {
        string user = HttpUtility.UrlDecode(parser.Parameters["user"]);
        string title = HttpUtility.UrlDecode(parser.Parameters["title"]);

        // Save the file somewhere
        File.WriteAllBytes(FILE_PATH + title + FILE_EXT, parser.FileContents);
    }
}
Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
Lorenzo Polidori
  • 10,332
  • 10
  • 51
  • 60
  • Thanks alot for your answer! I'll try to check it and hope that will support multipar/mixed format too! – Gohlool Feb 02 '12 at 23:24
  • 1
    @Gohlool yes, the HttpMultipartParser supports multipart form-data formed of an arbitrary number of text parts and a file part. The name of the part containing the file needs to be passed as second argument in the constructor and the file can be any mime-type. – Lorenzo Polidori Feb 03 '12 at 08:55
  • Btw, the `HttpMultipartParser` is distributed under the terms of the MIT License. – Lorenzo Polidori Mar 09 '12 at 10:03
  • I have tried your code, and it works good, except it fails to process binary attachment (application/octet-stream). This is caused by immediate conversion to `string`, and it becomes impossible to get the original bytes. It can be retrieve `contentTypeMatch.Index`, but `contentTypeMatch.Length` is wrong. Nor does it work for second and following attachments (I've changed a code to support it). Can you please suggest what to do? – Be Brave Be Like Ukraine Jan 13 '13 at 20:36
  • @bytebuster does this happen only with the `HttpContentParser`, or with the `HttpMultipartParser` too? – Lorenzo Polidori Jan 14 '13 at 09:09
  • `HttpMultipartParser` only, binary attachments only. – Be Brave Be Like Ukraine Jan 14 '13 at 10:16
  • @bytebuster you said you have changed the code to support it, can you please make a fork and a pull request? – Lorenzo Polidori Jan 14 '13 at 10:54
  • Most of the code for the (2012) MIT licensed code looks like it was lifted from the (2009) LGPL code. Same comments, variable names, style, etc. I am not a lawyer, but believe that derivative works require that the modified code also be released under LGPL. – Joe Zack Dec 31 '14 at 20:50
  • 4
    bitbucket link is broken – Avner Oct 09 '20 at 13:19
  • Link is broken... – A X Sep 05 '22 at 05:37
10

I've had some issues with parser that are based on string parsing particularly with large files I found it would run out of memory and fail to parse binary data.

To cope with these issues I've open sourced my own attempt at a C# multipart/form-data parser here

See my answer here for more information.

Community
  • 1
  • 1
Jake Woods
  • 1,808
  • 15
  • 20
  • That is the best C# multipart/form-data parser. I've used many of them but this one is definitely the best. – Arad Alvand Jun 09 '18 at 11:27
  • Your parser was very slick to use, however the nuget package forced me to update `System.ComponentModel.Annotations` which cascaded into upgrading Json, which totally broke my project. Not really your fault, but ultimately I wasnt able to use it. – crthompson Mar 13 '19 at 15:42
1

Check out the new MultipartStreamProvider and its subclasses (i.e. MultipartFormDataStreamProvider). You can create your own implementation too if none of the built in implementations are suitable for you use case.

user3285954
  • 4,499
  • 2
  • 27
  • 19
1

With Core now you have access to a IFormCollection by using HttpContext.Request.Form.

Example saving an image:

        Microsoft.AspNetCore.Http.IFormCollection form;
        form = ControllerContext.HttpContext.Request.Form; 

        using (var fileStream = System.IO.File.Create(strFile))
        {
            form.Files[0].OpenReadStream().Seek(0, System.IO.SeekOrigin.Begin);
            form.Files[0].OpenReadStream().CopyTo(fileStream);
        }
user6788933
  • 285
  • 2
  • 10
  • This doesn't actually answer the question asked but nonetheless is a good hint that allowed me to find this `IFormCollection` property. This is great and lightweight if you're on .NET Core. – Felipe Romero Jul 13 '19 at 16:35
0

I had a similar problem that i recently solved thanks to Anthony over at http://antscode.blogspot.com/ for the multipart parser.

Uploading file from Flex to WCF REST Stream issues (how to decode multipart form post in REST WS)

Community
  • 1
  • 1
Ed Sinek
  • 4,829
  • 10
  • 53
  • 81