public static void ParseFiles(Stream stream, string[] filenames, Encoding encoding, MultiPartFileHandler multiPartHandler) { List<string> lowerCasedFileNames = new List<string>(); foreach (string fname in filenames) { lowerCasedFileNames.Add(fname.ToLower()); } // Read the stream into a byte array byte[] data = ArrayHelper.ToByteArray(stream); // Copy to a string for header parsing string content = encoding.GetString(data); System.IO.File.WriteAllText(@"C:\debug.txt", content); // 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 currentSection in sections) { if (currentSection.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(currentSection); string name = nameMatch.Value.Trim(); int idx = lowerCasedFileNames.IndexOf(name.ToLower()); if (idx >= 0) { // Look for Content-Type Regex re = new Regex(@"(?<=Content\-Type:)(.*?)(?=\r\n\r\n)"); Match contentTypeMatch = re.Match(currentSection); // Look for filename re = new Regex(@"(?<=filename\=\"")(.*?)(?=\"")"); Match filenameMatch = re.Match(currentSection); // Did we find the required values? if (contentTypeMatch.Success && filenameMatch.Success) { // Set properties string contentType = contentTypeMatch.Value.Trim(); string fileName = filenameMatch.Value.Trim(); System.IO.File.WriteAllText(@"C:\debug2.txt", fileName); if (multiPartHandler != null) { // 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 = ArrayHelper.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); multiPartHandler(fileName, contentType, 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()); //} } } } }
public static void ParseFiles(Stream stream, string[] filenames, Encoding encoding, MultiPartFileHandler multiPartHandler) { List <string> lowerCasedFileNames = new List <string>(); foreach (string fname in filenames) { lowerCasedFileNames.Add(fname.ToLower()); } // Read the stream into a byte array byte[] data = ArrayHelper.ToByteArray(stream); // Copy to a string for header parsing string content = encoding.GetString(data); System.IO.File.WriteAllText(@"C:\debug.txt", content); // 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 currentSection in sections) { if (currentSection.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(currentSection); string name = nameMatch.Value.Trim(); int idx = lowerCasedFileNames.IndexOf(name.ToLower()); if (idx >= 0) { // Look for Content-Type Regex re = new Regex(@"(?<=Content\-Type:)(.*?)(?=\r\n\r\n)"); Match contentTypeMatch = re.Match(currentSection); // Look for filename re = new Regex(@"(?<=filename\=\"")(.*?)(?=\"")"); Match filenameMatch = re.Match(currentSection); // Did we find the required values? if (contentTypeMatch.Success && filenameMatch.Success) { // Set properties string contentType = contentTypeMatch.Value.Trim(); string fileName = filenameMatch.Value.Trim(); System.IO.File.WriteAllText(@"C:\debug2.txt", fileName); if (multiPartHandler != null) { // 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 = ArrayHelper.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); multiPartHandler(fileName, contentType, 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()); //} } } } }