protected async Task <UploadModel> Upload() { var formOptions = new FormOptions(); var result = new UploadModel(); result.Boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType), formOptions.MultipartBoundaryLengthLimit); var reader = new MultipartReader(result.Boundary, HttpContext.Request.Body); var section = (MultipartSection)null; while ((section = await reader.ReadNextSectionAsync()) != null) { if (ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition)) { if (MultipartRequestHelper.HasFile(contentDisposition)) { result.FileName = contentDisposition.FileName.ToString(); result.Stream = section.Body; return(result); } else if (MultipartRequestHelper.HasModel(contentDisposition)) { using (var factory = new DBItemConverterFactory(HttpContext)) { var option = new JsonSerializerOptions(); option.InitDefaults(factory); result.Model = await JsonSerializer.DeserializeAsync <T>(section.Body, option); } } else if (MultipartRequestHelper.HasFormData(contentDisposition)) { var key = HeaderUtilities.RemoveQuotes(contentDisposition.Name); var encoding = MultipartRequestHelper.GetEncoding(section); using (var streamReader = new StreamReader(section.Body, encoding, detectEncodingFromByteOrderMarks: true, bufferSize: 2048, leaveOpen: true)) { // The value length limit is enforced by MultipartBodyLengthLimit var value = await streamReader.ReadToEndAsync(); if (String.Equals(value, "undefined", StringComparison.OrdinalIgnoreCase)) { value = String.Empty; } if (StringSegment.Equals(key, "LastWriteTime", StringComparison.OrdinalIgnoreCase) && DateTime.TryParseExact(value, "o", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var lastWriteTime)) { result.ModificationDate = lastWriteTime; } else { result.Content[key.ToString()] = value; } } } } } return(result); }
public static void InitDefaults(this JsonSerializerOptions options, DBItemConverterFactory converter) { #if DEBUG options.WriteIndented = true; #endif options.IgnoreNullValues = false; options.IgnoreReadOnlyProperties = false; options.DefaultBufferSize = 82 * 1024; options.PropertyNamingPolicy = null; options.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping; // Configure a converters. //options.Converters.Add(new JsonStringEnumConverter()); options.Converters.Add(new TimeSpanConverter()); options.Converters.Add(converter); }
public sealed override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (selectedEncoding == null) { throw new ArgumentNullException(nameof(selectedEncoding)); } var httpContext = context.HttpContext; httpContext.DisableBuffering(); var option = new JsonSerializerOptions(); using (var factory = new DBItemConverterFactory(httpContext)) { option.InitDefaults(factory); var objectType = context.Object?.GetType() ?? context.ObjectType ?? typeof(object); if (TypeHelper.IsEnumerable(objectType)) { var pipeWriter = httpContext.Response.BodyWriter; await WriteArray(pipeWriter, (IEnumerable)context.Object, objectType, option); await pipeWriter.FlushAsync(); } else { var pipeWriter = httpContext.Response.BodyWriter; using (var jsonWriter = CreatetWriter(pipeWriter, option)) { JsonSerializer.Serialize(jsonWriter, context.Object, objectType, option); } await pipeWriter.FlushAsync(); } } }
public DBItemConverter(DBItemConverterFactory factory) { Factory = factory; }