public virtual IActionResult EquipmentIdAttachmentsPost([FromRoute] int id, [FromForm] IList <IFormFile> files) { // validate the id bool exists = _context.HetEquipment.Any(a => a.EquipmentId == id); if (!exists) { return(new StatusCodeResult(404)); } HetEquipment equipment = _context.HetEquipment .Include(x => x.HetDigitalFile) .First(a => a.EquipmentId == id); foreach (IFormFile file in files) { if (file.Length > 0) { HetDigitalFile attachment = new HetDigitalFile(); // strip out extra info in the file name if (!string.IsNullOrEmpty(file.FileName)) { attachment.FileName = Path.GetFileName(file.FileName); } // allocate storage for the file and create a memory stream attachment.FileContents = new byte[file.Length]; using (MemoryStream fileStream = new MemoryStream(attachment.FileContents)) { file.CopyTo(fileStream); } attachment.Type = GetType(attachment.FileName); // set the mime type id int?mimeTypeId = StatusHelper.GetMimeTypeId(attachment.Type, _context); if (mimeTypeId == null) { throw new DataException("Mime Type Id cannot be null"); } attachment.MimeTypeId = (int)mimeTypeId; equipment.HetDigitalFile.Add(attachment); } } _context.SaveChanges(); return(new ObjectResult(equipment.HetDigitalFile)); }