public static FormResult ReadForm(byte[] input) { FormResult result = new FormResult(); string boundary = Boundary.GetBoundary(input); if (!string.IsNullOrEmpty(boundary)) { var reader = new Microsoft.AspNet.WebUtilities.MultipartReader(boundary, new MemoryStream(input)); var next = reader.ReadNextSectionAsync(); var sectionResult = next.Result; while (sectionResult != null) { var attributes = AttributeReader.GetAttributes(sectionResult.ContentDisposition); MemoryStream memory = new MemoryStream(); sectionResult.Body.CopyTo(memory); if (attributes.ContainsKey("filename")) { File file = new File(); file.Bytes = memory.ToArray(); file.ContentType = sectionResult.ContentType; if (attributes.ContainsKey("filename")) { file.FileName = attributes["filename"]; } else if (attributes.ContainsKey("name")) { file.FileName = attributes["name"]; } result.Files.Add(file); } else if (attributes.ContainsKey("name")) { string parakey = attributes["name"]; if (!string.IsNullOrEmpty(parakey)) { var bytes = memory.ToArray(); var encoding = Helper.EncodingDetector.GetEncoding(ref bytes); string paravalue = encoding.GetString(bytes); result.FormData.Add(parakey, paravalue); } } next = reader.ReadNextSectionAsync(); if (next == null) { break; } sectionResult = next.Result; } } return(result); }
public static List <File> ReadFile(byte[] input) { List <File> result = new List <File>(); string boundary = Boundary.GetBoundary(input); if (!string.IsNullOrEmpty(boundary)) { var reader = new Microsoft.AspNet.WebUtilities.MultipartReader(boundary, new MemoryStream(input)); var next = reader.ReadNextSectionAsync(); var sectionResult = next.Result; while (sectionResult != null) { File file = new File(); MemoryStream memory = new MemoryStream(); sectionResult.Body.CopyTo(memory); file.Bytes = memory.ToArray(); file.ContentType = sectionResult.ContentType; var attributes = AttributeReader.GetAttributes(sectionResult.ContentDisposition); if (attributes.ContainsKey("filename")) { file.FileName = attributes["filename"]; } else if (attributes.ContainsKey("name")) { file.FileName = attributes["name"]; } result.Add(file); next = reader.ReadNextSectionAsync(); if (next == null) { break; } sectionResult = next.Result; } } return(result); }