private void Parse(MemoryStream stream, Encoding encoding) { this.Success = false; // Read the stream into a byte array byte[] data = Misc.ToByteArray(stream); // Copy to a string for header parsing string content = encoding.GetString(data); // The first line should contain the delimiter int delimiterEndIndex = content.IndexOf("\r\n"); if (delimiterEndIndex > -1) { string delimiter = content.Substring(0, content.IndexOf("\r\n")); string[] sections = content.Split(new string[] { delimiter }, StringSplitOptions.RemoveEmptyEntries); foreach (string s in sections) { if (s.Contains("Content-Disposition")) { // If we find "Content-Disposition", this is a valid multi-part section // Now, look for the "name" parameter Match nameMatch = new Regex(@"(?<=name\=\"")(.*?)(?=\"")").Match(s); string name = nameMatch.Value.Trim().ToLower(); if (name == FilePartName) { // Look for Content-Type Regex re = new Regex(@"(?<=Content\-Type:)(.*?)(?=\r\n\r\n)"); Match contentTypeMatch = re.Match(content); // Look for filename re = new Regex(@"(?<=filename\=\"")(.*?)(?=\"")"); Match filenameMatch = re.Match(content); // Did we find the required values? if (contentTypeMatch.Success && filenameMatch.Success) { // Set properties this.ContentType = contentTypeMatch.Value.Trim(); this.Filename = filenameMatch.Value.Trim(); // Get the start & end indexes of the file contents int startIndex = contentTypeMatch.Index + contentTypeMatch.Length + "\r\n\r\n".Length; byte[] delimiterBytes = encoding.GetBytes("\r\n" + delimiter); int endIndex = Misc.IndexOf(data, delimiterBytes, startIndex); int contentLength = endIndex - startIndex; // Extract the file contents from the byte array byte[] fileData = new byte[contentLength]; Buffer.BlockCopy(data, startIndex, fileData, 0, contentLength); this.FileContents = fileData; } } else if (!string.IsNullOrWhiteSpace(name)) { // Get the start & end indexes of the file contents int startIndex = nameMatch.Index + nameMatch.Length + "\r\n\r\n".Length; Parameters.Add(name, s.Substring(startIndex).TrimEnd(new char[] { '\r', '\n' }).Trim()); } } } // If some data has been successfully received, set success to true if (FileContents != null || Parameters.Count != 0) { this.Success = true; } } }