C# 中是否有任何多部分/表单数据解析器 -(无 ASP)

发布于 2024-09-27 03:10:39 字数 123 浏览 1 评论 0原文

我只是想编写一个多部分解析器,但事情变得复杂,想问是否有人知道 C# 中有一个现成的解析器!

只是为了澄清,我正在编写自己的“小型”http 服务器,并且也需要解析多部分表单数据!

提前致谢, 戈洛尔

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

┊风居住的梦幻卍 2024-10-04 03:10:39

我在此处开源了一个 C# Http 表单解析器。

这比 CodePlex 上提到的另一个稍微灵活一些,因为您可以将它用于多部分和非多部分 form-data,而且它还为您提供了以 格式设置的其他表单参数。代码>字典对象。

可以按如下方式使用:

非多部分

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

多部分

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

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);
    }
}
寂寞清仓 2024-10-04 03:10:39

我遇到了一些基于字符串解析的解析器问题,特别是对于大文件,我发现它会耗尽内存并且无法解析二进制数据。

为了解决这些问题,我开源了我自己对 C# multipart/form-data 解析器的尝试 此处

请参阅我的回答此处了解更多信息。

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.

饭团 2024-10-04 03:10:39

查看新的 MultipartStreamProvider 及其子类(即 MultipartFormDataStreamProvider)。如果没有任何内置实现适合您的用例,您也可以创建自己的实现。

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.

音盲 2024-10-04 03:10:39

现在,使用 Core,您可以使用 HttpContext.Request.Form 访问 IFormCollection。

保存图像的示例:

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

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);
        }
浪推晚风 2024-10-04 03:10:39

我有一个类似的问题,我最近解决了,感谢安东尼在 http://antscode.blogspot.com/ 用于多部分解析器。

从 Flex 上传文件WCF REST Stream 问题(如何在 REST WS 中解码多部分表单发布)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文