public void TestParse_Base64_Binary() { var content = "--7a1d30f95d847a130f0e77ca\n" + "Content-Disposition: form-data; name=\"this_is_name\"; filename=\"this_is_filename\"\n" + "Content-Transfer-Encoding: Base64\n" + "Content-Type: application/octet-stream\n\n" + "SGVsbG8gVGhpcyBpcyBhIGNvbnRlbnQ=\n" + // reads 'Hello This is a content' "--7a1d30f95d847a130f0e77ca"; var raw = Encoding.UTF8.GetBytes(content); using (var stream = new MemoryStream()) { stream.Write(raw, 0, raw.Length); stream.Position = 0; var parser = new HTTPMultipartParser(stream, EFileHandlingType.ALL_STREAMED); var part = parser.Parse().ElementAt(0); Assert.AreEqual("this_is_filename", part.FileName); Assert.AreEqual("this_is_name", part.Name); Assert.IsTrue(part.IsBinary); Assert.AreEqual("Base64", part.ContentTransferEncoding); Assert.AreEqual("application/octet-stream", part.ContentType); var bytes = (byte[])part.GetData(); var stringContent = Encoding.UTF8.GetString(bytes); Assert.AreEqual("Hello This is a content", stringContent); } }
public void TestParse_ContentDisposition_NameFirst() { var content = "--7a1d30f95d847a130f0e77ca\n" + "Content-Disposition: form-data; name=\"this_is_name\"; filename=\"this_is_filename\"\n" + "Content-Type: text/plain\n\n" + "This is a content\n" + "--7a1d30f95d847a130f0e77ca"; var raw = Encoding.UTF8.GetBytes(content); using (var stream = new MemoryStream()) { stream.Write(raw, 0, raw.Length); stream.Position = 0; var parser = new HTTPMultipartParser(stream, EFileHandlingType.ALL_STREAMED); var part = parser.Parse().ElementAt(0); Assert.AreEqual("this_is_filename", part.FileName); Assert.AreEqual("this_is_name", part.Name); Assert.AreEqual("This is a content", (string)part.GetData()); Assert.AreEqual("text/plain", part.ContentType); } }
private Stream TestFileUpload(Stream body, HTTPMultipartParser parser) { if (step_through) { //This code was made for walking^Wstepping through... string a = ""; foreach (StreamedFileData fileData in parser.Parse()) { if (fileData.IsBinary) { byte[] x = (byte[])fileData.GetData(); } else { a += fileData.Name + ": '" + (string)fileData.GetData() + "'"; } } } else //Will save files to disk { foreach (StreamedFileData fileData in parser.Parse()) { fileData.ToFile(Path.Combine(dir_path, fileData.FileName)); } } StreamWriter fieldFile = null; if (!step_through) { //Create a file to save non-file fields fieldFile = File.CreateText(Path.Combine(dir_path, "__multipart_fields.txt")); } //Save/step through any other fields/buffered files foreach (var kvp in parser.Fields) { string name = kvp.Key; MultipartData field = kvp.Value; if (!step_through) { if (field.IsBinary) { File.WriteAllBytes(Path.Combine(dir_path, field.FileName ?? field.Name), ((BinaryData)field).Data); } else { if (field.IsFile) { File.WriteAllText(Path.Combine(dir_path, field.FileName ?? field.Name), ((TextData)field).Data); } else { fieldFile.WriteLine("[ ===== " + field.Name + " ====]"); fieldFile.WriteLine(((TextData)field).Data); fieldFile.WriteLine(); } } } } if (!step_through) { fieldFile.Close(); } return(null); }