C#中是否有任何multipart / form-data解析器 – (NO ASP)

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

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

在此先感谢Gohlool

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

这比在CodePlex上提到的另一个稍微灵活一些,因为您可以将它用于Multipart和非Multipart form-data ,并且它还为您提供在Dictionary对象中格式化的其他表单参数。

这可以使用如下:

非多

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

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

为了解决这些问题,我在这里开放了自己的C#multipart / form-data解析器

有关详细信息,请参阅我的答案。

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

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

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

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