public IActionResult UploadImage(ParkingImageModel model) { if (ModelState.IsValid) { string base64EncodedImage = ImageProcessing.ProcessIFormFile(model.Image); UploadLotImageModel dataModel = new UploadLotImageModel { Image = base64EncodedImage, ImageName = model.Image.FileName, ParkingLotId = model.ParkingLotId }; ResponseDetails response = _apiHelper.SendApiRequest(dataModel, "parkingLot/upload-image", HttpMethod.Post); if (response.Success) { return(RedirectToAction("Images", new { id = _dataProtector.Protect(model.ParkingLotId) })); } else { ErrorViewModel errorModel = new ErrorViewModel { Message = response.Data.ToString() }; return(View("Error", errorModel)); } } ModelState.AddModelError("", "Validation Error."); return(View(model)); }
public object UploadImage(UploadLotImageModel model) { ParkingLotImageViewModel imageModel = _parkingLotService.UploadImage(model, _path); if (imageModel == null) { return(new ResponseDetails(false, $"Parking lot with Id : { model.ParkingLotId } not found.")); } List <string> returnModel = _parkingLotService.GetImages(model.ParkingLotId, _path); return(new ResponseDetails(true, returnModel)); }
private string SaveImage(UploadLotImageModel model, string path) { string uniqueName = (DateTime.Now.Ticks.ToString() + "_" + model.ImageName) .Replace("-", "_") .Replace(" ", "_"); string imagePath = Path.Combine(path, uniqueName); byte[] bytes = Convert.FromBase64String(model.Image); File.WriteAllBytes(imagePath, bytes); return(uniqueName); }
public ParkingLotImageViewModel UploadImage(UploadLotImageModel model, string path) { if (_unitOfWork.ParkingLotRepository.ParkingLotExists(model.ParkingLotId)) { string uniqueName = SaveImage(model, path); ParkingLotImage imageModel = new ParkingLotImage() { ImageName = uniqueName, ParkingLotId = model.ParkingLotId }; imageModel = _unitOfWork.ParkingLotRepository.AddImage(imageModel); _unitOfWork.SaveChanges(); return(_mapper.Map <ParkingLotImageViewModel> (imageModel)); } return(null); }