/// <summary> /// Creates a new instance of the <see cref="FileMultipartSection"/> class /// </summary> /// <param name="section">The section from which to create the <see cref="FileMultipartSection"/></param> /// <param name="header">An already parsed content disposition header</param> public FileMultipartSection(MultipartSection section, ContentDispositionHeaderValue header) { if (!header.IsFileDisposition()) { throw new ArgumentException($"Argument must be a file section", nameof(section)); } Section = section; _contentDispositionHeader = header; Name = HeaderUtilities.RemoveQuotes(_contentDispositionHeader.Name).ToString(); FileName = HeaderUtilities.RemoveQuotes( _contentDispositionHeader.FileNameStar.HasValue ? _contentDispositionHeader.FileNameStar : _contentDispositionHeader.FileName).ToString(); }
/// <summary> /// Reads the body of the section as a string /// </summary> /// <param name="section">The section to read from</param> /// <returns>The body steam as string</returns> public static async Task <string> ReadAsStringAsync(this MultipartSection section) { if (section == null) { throw new ArgumentNullException(nameof(section)); } MediaTypeHeaderValue.TryParse(section.ContentType, out var sectionMediaType); var streamEncoding = sectionMediaType?.Encoding; if (streamEncoding == null || streamEncoding == Encoding.UTF7) { streamEncoding = Encoding.UTF8; } using (var reader = new StreamReader( section.Body, streamEncoding, detectEncodingFromByteOrderMarks: true, bufferSize: 1024, leaveOpen: true)) return(await reader.ReadToEndAsync()); }
/// <summary> /// Creates a new instance of the <see cref="FormMultipartSection"/> class /// </summary> /// <param name="section">The section from which to create the <see cref="FormMultipartSection"/></param> /// <remarks>Reparses the content disposition header</remarks> public FormMultipartSection(MultipartSection section) : this(section, section.GetContentDispositionHeader()) { }
/// <summary> /// Retrieves and parses the content disposition header from a section /// </summary> /// <param name="section">The section from which to retrieve</param> /// <returns>A <see cref="ContentDispositionHeaderValue"/> if the header was found, null otherwise</returns> public static ContentDispositionHeaderValue GetContentDispositionHeader(this MultipartSection section) => !ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var header) ? null : header;