Esempio n. 1
0
        public IActionResult Add([FromForm] FileUpload file)
        {
            string path = _webHostEnvironment.WebRootPath + "\\images\\";

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            string imageName    = Guid.NewGuid().ToString();
            string fullPath     = path + imageName;
            var    businessRule = _carImageService.CheckIfImageLimitExceded(file.CarId);

            if (businessRule.Success)
            {
                using (FileStream fs = System.IO.File.Create(fullPath))
                {
                    file.files.CopyTo(fs);
                    fs.Flush();
                }
            }
            CarImage carImage = new CarImage {
                ImagePath = fullPath, CarId = file.CarId, Date = DateTime.Now
            };
            var result = _carImageService.Add(carImage);

            if (result.Success)
            {
                return(Ok(result));
            }
            return(BadRequest(result));
        }