public static T ReadAs <T>(this MultipartSection section, JsonSerializerSettings jsonSerializerSettings = null)
        {
            using (var streamReader = new StreamReader(section.Body))
                using (var jsonReader = new JsonTextReader(streamReader))
                {
                    var serializer =
                        jsonSerializerSettings == null
          ? JsonSerializer.CreateDefault()
          : JsonSerializer.Create(jsonSerializerSettings);

                    var model = serializer.Deserialize <T>(jsonReader);
                    return(model);
                }
        }
        /// <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();
        }
示例#3
0
        /// <summary>
        /// Converts the section to a form section
        /// </summary>
        /// <param name="section">The section to convert</param>
        /// <returns>A form section</returns>
        public static FormMultipartSection?AsFormDataSection(this MultipartSection section)
        {
            if (section == null)
            {
                throw new ArgumentNullException(nameof(section));
            }

            try
            {
                return(new FormMultipartSection(section));
            }
            catch
            {
                return(null);
            }
        }
示例#4
0
 /// <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())
 {
 }