public IActionResult Upload([FromForm] IFormFile file) { try { var folderName = Path.Combine("StaticFiles", "Images"); var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName); var id = Guid.NewGuid(); var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); var fullPath = Path.Combine(pathToSave, id.ToString() + ".jpg"); var filepath = Path.Combine(folderName, fileName); var memoryStream = new MemoryStream(); file.CopyTo(memoryStream); using (var stream = new FileStream(fullPath, FileMode.Create)) { file.CopyTo(stream); } var image = new Image { Id = id, ImgPath = filepath }; _context.Add(image); _context.SaveChanges(); Console.WriteLine(filepath); return(Ok(new { filepath })); } catch (Exception ex) { return(StatusCode(500, $"Internal server error: {ex}")); } }
/// <summary> /// Select or deselect an image - adding or removing it from the basket. /// </summary> /// <param name="image"></param> /// <param name="newState"></param> public void SetBasketState(Image image, bool newState) { using var db = new ImageContext(); bool changed = false; var watch = new Stopwatch("SetSelection"); var entry = db.BasketEntries.FirstOrDefault(x => x.ImageId.Equals(image.ImageId)); if (newState && entry == null) { entry = new BasketEntry { ImageId = image.ImageId, BasketId = CurrentBasket.BasketId, DateAdded = DateTime.UtcNow }; db.Add(entry); db.SaveChanges("AddToBasket"); image.BasketEntry = entry; SelectedImages.Add(image); changed = true; StatusService.Instance.StatusText = $"{image.FileName} added to the basket."; } else if (!newState && entry != null) { db.Remove(entry); db.SaveChanges("RemoveFromBasket"); image.BasketEntry = null; SelectedImages.Remove(image); changed = true; StatusService.Instance.StatusText = $"{image.FileName} removed from the basket."; } watch.Stop(); if (changed) { NotifyStateChanged(); } }
public void DatabaseAdding(ObjectInImageProbability image) { ImageInformation AddedImage = new ImageInformation { Path = image.Path, ImageContext = new Blob() { ImageContext = File.ReadAllBytes(image.Path) }, Probability = image.Probability }; var query = ClassLabels.Include(item => item.ImagesInformation).Where(item => item.StringClassLabel == image.ClassLabel); if (query.Count() == 0) { AddedImage.ClassLabel = new ClassLabel() { StringClassLabel = image.ClassLabel, ClassLabelImagesNumber = 1 }; AddedImage.ClassLabel.ImagesInformation = new List <ImageInformation> { AddedImage }; ImagesInformation.Add(AddedImage); ClassLabels.Add(AddedImage.ClassLabel); ImageContext.Add(AddedImage.ImageContext); } else { AddedImage.ClassLabel = query.First(); AddedImage.ClassLabel.ImagesInformation.Add(AddedImage); AddedImage.ClassLabel.ClassLabelImagesNumber++; ImagesInformation.Add(AddedImage); ImageContext.Add(AddedImage.ImageContext); } SaveChanges(); }