Example #1
0
        private static async Task writeFileAsync(StreamWriter writer, MultiPartFile file)
        {
            string header = getFileSectionHeader(file);
            await writer.WriteAsync(header);

            await file.Writer.WriteAsync(writer.BaseStream);

            await writer.WriteAsync(newLine);
        }
Example #2
0
        private static void writeFile(StreamWriter writer, MultiPartFile file)
        {
            string header = getFileSectionHeader(file);

            writer.Write(header);

            file.Writer.Write(writer.BaseStream);
            writer.Write(newLine);
        }
Example #3
0
        public IMultiPartBodyBuilder WithFile(string name, string filePath, string contentType = null, NameValueCollection headers = null)
        {
            MultiPartFile file = new MultiPartFile()
            {
                Name        = name,
                FileName    = Path.GetFileName(filePath),
                ContentType = contentType,
                Writer      = MultiPartFile.GetPathWriter(filePath),
                Headers     = headers ?? new NameValueCollection()
            };

            fileStreams.Add(file);
            return(this);
        }
Example #4
0
        public IMultiPartBodyBuilder WithFile(string name, string fileName, byte[] content, string contentType = null, NameValueCollection headers = null)
        {
            MultiPartFile file = new MultiPartFile()
            {
                Name        = name,
                FileName    = fileName,
                ContentType = contentType,
                Writer      = MultiPartFile.GetStreamWriter(new MemoryStream(content)),
                Headers     = headers ?? new NameValueCollection()
            };

            fileStreams.Add(file);
            return(this);
        }
Example #5
0
        private static string getFileSectionHeader(MultiPartFile file)
        {
            WebHeaderCollection headers = new WebHeaderCollection();

            headers.Add(file.Headers);
            headers.Add("Content-Disposition", "form-data");
            headers.Add("Content-Disposition", "name=\"" + escapeQuotes(file.Name) + "\"");
            headers.Add("Content-Disposition", "filename=\"" + escapeQuotes(file.FileName) + "\"");
            headers.Add("Content-Type", file.ContentType ?? "application/octet-stream");

            StringBuilder builder = new StringBuilder();

            builder.Append("--" + Boundary + newLine);

            writeHeaders(builder, headers);

            return(builder.ToString());
        }
Example #6
0
 public void Add(string name, MultiPartFile file)
 {
     if (file == null)
     {
         throw new ArgumentNullException("file");
     }
     if (name == null)
     {
         nullFiles.Add(file);
     }
     else
     {
         List <MultiPartFile> files;
         if (!fileLookup.TryGetValue(name ?? String.Empty, out files))
         {
             files = new List <MultiPartFile>();
             fileLookup.Add(name ?? String.Empty, files);
         }
         files.Add(file);
     }
 }
 public void Add(string name, MultiPartFile file)
 {
     if (file == null)
     {
         throw new ArgumentNullException("file");
     }
     if (name == null)
     {
         nullFiles.Add(file);
     }
     else
     {
         List<MultiPartFile> files;
         if (!fileLookup.TryGetValue(name ?? String.Empty, out files))
         {
             files = new List<MultiPartFile>();
             fileLookup.Add(name ?? String.Empty, files);
         }
         files.Add(file);
     }
 }
        public static MultiPartResponse FromMultiPart(this IWebResponse response)
        {
            if (response == null)
            {
                throw new ArgumentNullException("response");
            }
            // Make sure it is a multi-part request
            string[] parts = response.Response.ContentType.Split(';').Select(s => s.Trim()).ToArray();
            if (!parts[0].Equals(ContentTypePrefix, StringComparison.InvariantCultureIgnoreCase))
            {
                return new MultiPartResponse() { Files = new MultiPartFileLookup(), FormData = new NameValueCollection() };
            }
            // Parse the content type parameters
            var contentTypeParameters = parts
                .Skip(1)
                .Select(p => p.Split(new char[] { '=' }, 2))
                .Where(p => p.Length == 2)
                .ToLookup(p => p[0], p => p[1], StringComparer.InvariantCultureIgnoreCase);
            // Check the boundary is specified, and only once
            if (contentTypeParameters["boundary"].Count() != 1)
            {
                return new MultiPartResponse() { Files = new MultiPartFileLookup(), FormData = new NameValueCollection() };
            }
            string boundary = contentTypeParameters["boundary"].First();

            using (Stream responseStream = response.Response.GetResponseStream())
            {
                Encoding encoding = response.Response.ContentEncoding == null
                    ? Encoding.UTF8
                    : Encoding.GetEncoding(response.Response.ContentEncoding);
                StreamingMultiPartParser parser = new StreamingMultiPartParser(responseStream, encoding, boundary);

                MultiPartFileLookup fileLookup = new MultiPartFileLookup();
                NameValueCollection collection = new NameValueCollection();
                parser.SectionFound += (o, e) =>
                {
                    var data = getSectionData(e);
                    if (data == null)
                    {
                        return;
                    }
                    if (String.IsNullOrWhiteSpace(data.FileName))
                    {
                        string value = encoding.GetString(data.Contents);
                        collection.Add(data.Name, value);
                    }
                    else
                    {
                        var file = new MultiPartFile()
                        {
                            Name = data.Name,
                            FileName = data.FileName,
                            ContentType = data.ContentType,
                            Contents = data.Contents
                        };
                        fileLookup.Add(file.Name, file);
                    }
                };
                return new MultiPartResponse() { Files = fileLookup, FormData = collection };
            }
        }
Example #9
0
        public static MultiPartResponse FromMultiPart(this IWebResponse response)
        {
            if (response == null)
            {
                throw new ArgumentNullException("response");
            }
            // Make sure it is a multi-part request
            string[] parts = response.Response.ContentType.Split(';').Select(s => s.Trim()).ToArray();
            if (!parts[0].Equals(ContentTypePrefix, StringComparison.InvariantCultureIgnoreCase))
            {
                return(new MultiPartResponse()
                {
                    Files = new MultiPartFileLookup(), FormData = new NameValueCollection()
                });
            }
            // Parse the content type parameters
            var contentTypeParameters = parts
                                        .Skip(1)
                                        .Select(p => p.Split(new char[] { '=' }, 2))
                                        .Where(p => p.Length == 2)
                                        .ToLookup(p => p[0], p => p[1], StringComparer.InvariantCultureIgnoreCase);

            // Check the boundary is specified, and only once
            if (contentTypeParameters["boundary"].Count() != 1)
            {
                return(new MultiPartResponse()
                {
                    Files = new MultiPartFileLookup(), FormData = new NameValueCollection()
                });
            }
            string boundary = contentTypeParameters["boundary"].First();

            using (Stream responseStream = response.Response.GetResponseStream())
            {
                Encoding encoding = response.Response.ContentEncoding == null
                    ? Encoding.UTF8
                    : Encoding.GetEncoding(response.Response.ContentEncoding);
                StreamingMultiPartParser parser = new StreamingMultiPartParser(responseStream, encoding, boundary);

                MultiPartFileLookup fileLookup = new MultiPartFileLookup();
                NameValueCollection collection = new NameValueCollection();
                parser.SectionFound += (o, e) =>
                {
                    var data = getSectionData(e);
                    if (data == null)
                    {
                        return;
                    }
                    if (String.IsNullOrWhiteSpace(data.FileName))
                    {
                        string value = encoding.GetString(data.Contents);
                        collection.Add(data.Name, value);
                    }
                    else
                    {
                        var file = new MultiPartFile()
                        {
                            Name        = data.Name,
                            FileName    = data.FileName,
                            ContentType = data.ContentType,
                            Contents    = data.Contents
                        };
                        fileLookup.Add(file.Name, file);
                    }
                };
                return(new MultiPartResponse()
                {
                    Files = fileLookup, FormData = collection
                });
            }
        }