public async Task <IActionResult> Index(IFormFile file) { if (file?.Length > 0) { string folderPath = _config.GetValue <string>("UploadImageDirectory"); var pictureName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName).ToLower(); string webRootPath = _webHostEnvironment.WebRootPath; string route = Path.Combine(webRootPath, folderPath, pictureName); using (var stream = new FileStream(route, FileMode.Create)) { await file.CopyToAsync(stream); } using (var fs = new FileStream(route, FileMode.Open)) { var picture = await _faceHelper.DetectAndExtractFaceAsync(fs); using (var image = Image.FromStream(file.OpenReadStream())) { picture.Width = image.Width; picture.Height = image.Height; } picture.Name = file.FileName; picture.Path = $"{folderPath}/{pictureName}"; _context.Pictures.Add(picture); await _context.SaveChangesAsync(); return(RedirectToAction("Details", "Pictures", new { Id = picture.Id })); } } return(View()); }