Exemplo n.º 1
0
        public void TestMultipartStreaming()
        {
            List <MultiPartSection> sections = new List <MultiPartSection>();

            //Input Stream (Normally NetworkStream)
            using (MemoryStream stream = new MemoryStream(exampleBytes))
                //MultiPartStream For reading sections in-stream
                using (MultiPartStream mStream = new MultiPartStream(stream))
                    //Files to write to
                    using (FileStream f1 = new FileStream("File1.txt", FileMode.Create))
                        using (FileStream f2 = new FileStream("File2.txt", FileMode.Create))
                        {
                            //Reads a section and handles found file blocks
                            MultiPartSection section;
                            while ((section = mStream.ReadSection((sect, data, length) =>
                            {
                                //Section with name file1
                                if (sect.Name == "file1")
                                {
                                    f1.Write(data, 0, (int)length);
                                }
                                //Section with name file2
                                if (sect.Name == "file2")
                                {
                                    f2.Write(data, 0, (int)length);
                                }
                            })) != null)
                            {
                                //Add it to the section list
                                if (section != null)
                                {
                                    sections.Add(section);
                                }
                            }
                        }

            foreach (MultiPartSection section in sections)
            {
                System.Console.WriteLine("New Section");
                System.Console.WriteLine($"FileName: {section.FileName}");
                System.Console.WriteLine($"Name: {section.Name}");
                System.Console.WriteLine($"Content-Type: {section.ContentType}");
                System.Console.WriteLine($"Content: {((!section.Streamed) ? section.DataAsString : "")}");
                System.Console.WriteLine($"Streamed: {section.Streamed}");
                System.Console.WriteLine("-------------------");
            }


            FileInfo fOut1 = new FileInfo("File1.txt");
            FileInfo fOut2 = new FileInfo("File2.txt");

            Assert.AreEqual(Encoding.UTF8.GetBytes(section2Data).Length, fOut1.Length, "Malformed data");
            Assert.AreEqual(Encoding.UTF8.GetBytes(section3Data).Length, fOut2.Length, "Malformed data");
        }
Exemplo n.º 2
0
        public void TestMultipartTestFileStreaming()
        {
            List <MultiPartSection> sections = new List <MultiPartSection>();

            //Input Stream (Normally NetworkStream)
            using (FileStream stream = new FileStream("TestMultipart", FileMode.Open))
                //MultiPartStream For reading sections in-stream
                using (MultiPartStream mStream = new MultiPartStream(stream))
                    //Files to write to
                    using (FileStream f1 = new FileStream("File1.txt", FileMode.Create))
                        using (FileStream f2 = new FileStream("File2.txt", FileMode.Create))
                        {
                            //Reads a section and handles found file blocks
                            MultiPartSection section;
                            while ((section = mStream.ReadSection((sect, data, length) =>
                            {
                                //Section with name file1
                                if (sect.Name == "file1")
                                {
                                    f1.Write(data, 0, (int)length);
                                }
                                //Section with name file2
                                if (sect.Name == "file2")
                                {
                                    f2.Write(data, 0, (int)length);
                                }
                            })) != null)
                            {
                                //Add it to the section list
                                if (section != null)
                                {
                                    sections.Add(section);
                                }
                            }
                        }

            foreach (MultiPartSection section in sections)
            {
                System.Console.WriteLine("New Section");
                System.Console.WriteLine($"FileName: {section.FileName}");
                System.Console.WriteLine($"Name: {section.Name}");
                System.Console.WriteLine($"Content-Type: {section.ContentType}");
                System.Console.WriteLine($"Content: {((!section.Streamed) ? section.DataAsString : "")}");
                System.Console.WriteLine($"Streamed: {section.Streamed}");
                System.Console.WriteLine("-------------------");
            }
        }