コード例 #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");
        }
コード例 #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("-------------------");
            }
        }
コード例 #3
0
        /// <summary>
        /// [ControllerPath]/TestMultipart?name=[SomeString]
        /// </summary>
        /// <param name="name">GET parameter</param>
        /// <param name="data">Multi-part/formdata body as MultiPartStream</param>
        /// <returns>Server.DefaultResponseType or AcceptHeader type of bool (With or without apiwrap)</returns>
        public string TestMultipart(string name, [Body(BodyType.MultipartStream)] MultiPartStream data)
        {
            List <MultiPartSection> sections;
            Stopwatch tstop = new Stopwatch();

            tstop.Start();
            using (FileStream str = new FileStream(name, FileMode.Create))
                sections = data.ReadAllSections((fileSection, buffer, read) =>
                {
                    if (!string.IsNullOrEmpty(fileSection.Name))
                    {
                        switch (fileSection.Name)
                        {
                        case "file1":
                            str.Write(buffer, 0, (int)read);
                            break;
                        }
                    }
                });

            tstop.Stop();
            return($"Uploaded in: {tstop.ElapsedMilliseconds}");
        }