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 virtual async Task <ActionResult <T> > UploadFileModel() { 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 upload = await Upload(); if (upload != null) { var item = upload.Model as T; if (item == null) { return(NotFound()); } if (IsDenied(item, transaction.Caller)) { item.Reject(transaction.Caller); return(Forbid()); } if (string.IsNullOrEmpty(item.GetValue <string>(table.FileNameKey))) { item.SetValue(upload.FileName, table.FileNameKey); } if (upload.Stream != null) { if (table.FileLastWriteKey != null && !item.IsChangedKey(table.FileLastWriteKey)) { item.SetValueNullable <DateTime>(upload.ModificationDate ?? DateTime.UtcNow, table.FileLastWriteKey); } 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); } } else { await item.Save(transaction); } transaction.Commit(); return(item); } return(BadRequest("Expect mutipart request name=file")); } catch (Exception ex) { transaction.Rollback(); return(BadRequest(ex)); } } }