public async Task<IActionResult> Post(IFormFile file) { if (file == null || file.Length == 0) { return BadRequest(); } var fileName = $"{Guid.NewGuid()}_{ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"')}"; var fullPath = Path.Combine(_scoringSvc.ImagesFolder, fileName); try { MemoryStream imageMemoryStream = new MemoryStream(); await file.CopyToAsync(imageMemoryStream); //Convert to Bitmap Bitmap bitmapImage = (Bitmap)Image.FromStream(imageMemoryStream); //Set the specific image data into the ImageInputData type used in the DataView ImageInputData imageInputData = new ImageInputData { Image = bitmapImage }; _logger.LogInformation($"Start processing image file { fullPath }"); var scoring = _scoringSvc.Score(imageInputData); _logger.LogInformation($"Image processed"); return Ok(ClassificationResponse.CreateFrom(scoring)); } finally { try { _logger.LogInformation($"Deleting Image {fullPath}"); System.IO.File.Delete(fullPath); } catch (Exception) { _logger.LogInformation("Error deleting image: " + fileName); } } }
public async Task<IActionResult> Post(IFormFile file) { if (file == null || file.Length == 0) { return BadRequest(); } var fileName = $"{Guid.NewGuid()}_{ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"')}"; var fullPath = Path.Combine(_scoringSvc.ImagesFolder, fileName); try { using (var fs = new FileStream(fullPath, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { await file.CopyToAsync(fs); } _logger.LogInformation($"Start processing image file { fullPath }"); var scoring = _scoringSvc.Score(fileName); _logger.LogInformation($"Image processed"); return Ok(ClassificationResponse.CreateFrom(scoring)); } finally { try { _logger.LogInformation($"Deleting Image {fullPath}"); System.IO.File.Delete(fullPath); } catch (Exception) { _logger.LogInformation("Error deleting image: " + fileName); } } }