protected IEnumerable <UploadModel> Upload() { var formAccumulator = new KeyValueAccumulator(); var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType), formOptions.MultipartBoundaryLengthLimit); var reader = new MultipartReader(boundary, HttpContext.Request.Body); var section = reader.ReadNextSectionAsync().GetAwaiter().GetResult(); while (section != null) { if (ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition)) { if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition)) { var result = new UploadModel { FileName = contentDisposition.FileName.ToString(), Stream = section.Body }; yield return(result); } else if (MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition)) { // Content-Disposition: form-data; name="key" // 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: 2048, leaveOpen: true)) { // The value length limit is enforced by MultipartBodyLengthLimit var value = streamReader.ReadToEnd(); if (String.Equals(value, "undefined", StringComparison.OrdinalIgnoreCase)) { value = String.Empty; } formAccumulator.Append(key.ToString(), value); if (formAccumulator.ValueCount > formOptions.ValueCountLimit) { throw new InvalidDataException($"Form key count limit {formOptions.ValueCountLimit} exceeded."); } } } } section = reader.ReadNextSectionAsync().GetAwaiter().GetResult(); } }
protected async Task <UploadModel> Upload(bool inMemory) { var formAccumulator = new KeyValueAccumulator(); var result = new UploadModel(); var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType), formOptions.MultipartBoundaryLengthLimit); var reader = new MultipartReader(boundary, HttpContext.Request.Body); var section = (MultipartSection)null; while ((section = await reader.ReadNextSectionAsync()) != null) { if (ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition)) { if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition)) { result.FileName = contentDisposition.FileName.ToString(); if (inMemory) { result.Stream = new MemoryStream(); } else { result.FilePath = Path.GetTempFileName(); result.Stream = System.IO.File.Create(result.FilePath); } await section.Body.CopyToAsync(result.Stream); await result.Stream.FlushAsync(); } 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: 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; } formAccumulator.Append(key.ToString(), value); if (formAccumulator.ValueCount > formOptions.ValueCountLimit) { throw new InvalidDataException($"Form key count limit {formOptions.ValueCountLimit} exceeded."); } } } } } return(result); }