private static async Task AddValueProviderAsync(ValueProviderFactoryContext context) { var request = context.ActionContext.HttpContext.Request; IFormCollection form; try { form = await request.ReadFormAsync(); } catch (InvalidDataException ex) { // ReadFormAsync can throw InvalidDataException if the form content is malformed. // Wrap it in a ValueProviderException that the CompositeValueProvider special cases. throw new ValueProviderException(Resources.FormatFailedToReadRequestForm(ex.Message), ex); } catch (IOException ex) { // ReadFormAsync can throw IOException if the client disconnects. // Wrap it in a ValueProviderException that the CompositeValueProvider special cases. throw new ValueProviderException(Resources.FormatFailedToReadRequestForm(ex.Message), ex); } var valueProvider = new FormValueProvider( BindingSource.Form, form, CultureInfo.CurrentCulture); context.ValueProviders.Add(valueProvider); }
private static async Task AddValueProviderAsync(ValueProviderFactoryContext context) { var request = context.ActionContext.HttpContext.Request; var valueProvider = new FormValueProvider( BindingSource.Form, await request.ReadFormAsync(), CultureInfo.CurrentCulture); context.ValueProviders.Add(valueProvider); }
protected override IEnumerableValueProvider GetEnumerableValueProvider( BindingSource bindingSource, Dictionary <string, StringValues> values, CultureInfo culture) { var emptyValueProvider = new QueryStringValueProvider(bindingSource, new QueryCollection(), culture); var valueProvider = new FormValueProvider(bindingSource, new FormCollection(values), culture); return(new CompositeValueProvider() { emptyValueProvider, valueProvider }); }
private static async Task AddValueProviderAsync(ValueProviderFactoryContext context) { var request = context.ActionContext.HttpContext.Request; IFormCollection form; try { form = await request.ReadFormAsync(); } catch (InvalidDataException ex) { throw new ValueProviderException(Resources.FormatFailedToReadRequestForm(ex.Message), ex); } var valueProvider = new FormValueProvider( BindingSource.Form, form, CultureInfo.CurrentCulture); context.ValueProviders.Add(valueProvider); }
public static async Task <Microsoft.AspNetCore.Mvc.ModelBinding.FormValueProvider> StreamFile(this HttpRequest request, Stream targetStream) { if (!MultipartRequestHelper.IsMultipartContentType(request.ContentType)) { throw new Exception($"Expected a multipart request, but got {request.ContentType}"); } // Used to accumulate all the form url encoded key value pairs in the // request. var formAccumulator = new KeyValueAccumulator(); string targetFilePath = null; var boundary = MultipartRequestHelper.GetBoundary( MediaTypeHeaderValue.Parse(request.ContentType), _defaultFormOptions.MultipartBoundaryLengthLimit); var reader = new MultipartReader(boundary, request.Body); var section = await reader.ReadNextSectionAsync(); while (section != null) { ContentDispositionHeaderValue contentDisposition; var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out contentDisposition); if (hasContentDispositionHeader) { if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition)) { await section.Body.CopyToAsync(targetStream); } else if (MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition)) { // Content-Disposition: form-data; name="key" // // value // Do not limit the key name length here because the // multipart headers length limit is already in effect. var key = HeaderUtilities.RemoveQuotes(contentDisposition.Name); var encoding = GetEncoding(section); using (var streamReader = new StreamReader( section.Body, encoding, detectEncodingFromByteOrderMarks: true, bufferSize: 1024, 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; } formAccumulator.Append(key.Trim().Value, value); if (formAccumulator.ValueCount > _defaultFormOptions.ValueCountLimit) { throw new InvalidDataException($"Form key count limit {_defaultFormOptions.ValueCountLimit} exceeded."); } } } } // Drains any remaining section body that has not been consumed and // reads the headers for the next section. section = await reader.ReadNextSectionAsync(); } // Bind form data to a model var formValueProvider = new Microsoft.AspNetCore.Mvc.ModelBinding.FormValueProvider( BindingSource.Form, new FormCollection(formAccumulator.GetResults()), CultureInfo.CurrentCulture); return(formValueProvider); }