Exemple #1
0
        /// <summary>
        /// Adds a file to a collection of System.Net.Http.HttpContent objects that get serialized to multipart/form-data MIME type.
        /// </summary>
        /// <param name="content">The HTTP content of multipart form data.</param>
        /// <param name="name">The name for the HTTP content to add.</param>
        /// <param name="file">The file info instance.</param>
        /// <param name="fileName">The file name for the HTTP content to add to the collection.</param>
        /// <param name="mediaType">The MIME of the file.</param>
        /// <return>The HTTP content to add.</return>
        public static StreamContent Add(this MultipartFormDataContent content, string name, FileInfo file, string fileName = null, string mediaType = null)
        {
            if (content == null || file == null)
            {
                return(null);
            }
            var c = new StreamContent(file.OpenRead());

            if (!string.IsNullOrWhiteSpace(mediaType))
            {
                c.Headers.ContentType = new MediaTypeHeaderValue(mediaType);
            }
            else if (mediaType == null)
            {
                var mime = WebFormat.GetMime(file);
                if (!string.IsNullOrWhiteSpace(mime))
                {
                    c.Headers.ContentType = new MediaTypeHeaderValue(mime);
                }
            }

            content.Add(c, name, fileName ?? file.Name);
            return(c);
        }