public override async Task <InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context) { var boundary = MediaTypeHeaderValue.Parse(context.HttpContext.Request.ContentType).Boundary.Value; var reader = new MultipartReader(boundary, context.HttpContext.Request.Body); var section = await reader.ReadNextSectionAsync(); ModelWithFile model = null; var file = new Models.File(); while (section != null) { var disposition = ContentDispositionHeaderValue.Parse(section.ContentDisposition); if (disposition.Name == "metadata") { model = JsonConvert.DeserializeObject <ModelWithFile>(await section.ReadAsStringAsync()); } if (disposition.Name == "document") { var stream = new MemoryStream(); var fileSection = section.AsFileSection(); fileSection.FileStream.CopyTo(stream); file.Contents = stream.ToArray(); file.MediaType = fileSection.FileName; } try { section = await reader.ReadNextSectionAsync(); } catch { section = null; } } model.MyFile = file; return(InputFormatterResult.Success(model)); }
public void Post([FromBody] ModelWithFile model) { }