Exemplo n.º 1
0
        /// <summary>
        /// Creates a part that represents a file attachment.
        /// </summary>
        /// <param name="name">The name of the form field.</param>
        /// <param name="fileName">Name of the file as the server should see it.</param>
        /// <param name="contentType">Type of the content in HTTP Content-Type format.</param>
        /// <param name="content">The content of the file.</param>
        /// <returns>The constructed part.</returns>
        public static MultipartPostPart CreateFormFilePart(string name, string fileName, string contentType, Stream content)
        {
            Requires.NotNullOrEmpty(name, "name");
            Requires.NotNullOrEmpty(fileName, "fileName");
            Requires.NotNullOrEmpty(contentType, "contentType");
            Requires.NotNull(content, "content");

            var part = new MultipartPostPart("file");

            try {
                part.ContentAttributes["name"]     = name;
                part.ContentAttributes["filename"] = fileName;
                part.PartHeaders[HttpRequestHeader.ContentType] = contentType;
                if (!contentType.StartsWith("text/", StringComparison.Ordinal))
                {
                    part.PartHeaders["Content-Transfer-Encoding"] = "binary";
                }

                part.Content = content;
                return(part);
            } catch {
                part.Dispose();
                throw;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a part that represents a simple form field.
        /// </summary>
        /// <param name="name">The name of the form field.</param>
        /// <param name="value">The value.</param>
        /// <returns>The constructed part.</returns>
        public static MultipartPostPart CreateFormPart(string name, string value)
        {
            Requires.NotNullOrEmpty(name, "name");
            Requires.NotNull(value, "value");

            var part = new MultipartPostPart("form-data");

            try {
                part.ContentAttributes["name"] = name;
                part.Content = new MemoryStream(Encoding.UTF8.GetBytes(value));
                return(part);
            } catch {
                part.Dispose();
                throw;
            }
        }