Are there any multipart/form-data parser in C# - (NO ASP)

后端 未结 5 480
礼貌的吻别
礼貌的吻别 2020-12-05 06:07

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 writin

5条回答
  •  执笔经年
    2020-12-05 06:40

    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);
        }
    }
    

提交回复
热议问题