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(); } }
public async Task <ActionResult> UploadFile([FromRoute] K id, [FromRoute] string fileName) { if (table.FileNameKey == null) { return(BadRequest("No file columns presented!")); } if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType)) { return(BadRequest($"Expected a multipart request, but got {Request.ContentType}")); } using (var transaction = new DBTransaction(table.Connection, CurrentUser)) { try { var item = table.LoadById(id, DBLoadParam.Load | DBLoadParam.Referencing, null, transaction); if (item == null) { return(NotFound()); } if (!(item.Access?.GetFlag(AccessType.Update, transaction.Caller) ?? true) && !(item.Access?.GetFlag(AccessType.Create, transaction.Caller) ?? true)) { return(Forbid()); } foreach (var upload in Upload()) { if (string.IsNullOrEmpty(fileName) && !string.IsNullOrEmpty(upload.FileName)) { fileName = upload.FileName; } item.SetValue(fileName, table.FileNameKey); if (table.FileLOBKey != null) { await item.SetLOB(upload.Stream, table.FileLOBKey, transaction); } else if (table.FileKey != null) { await item.SetStream(upload.Stream, table.FileKey, transaction); } transaction.Commit(); } return(Ok()); } catch (Exception ex) { transaction.Rollback(); return(BadRequest(ex)); } } }
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); }