Пример #1
0
        public ActionResult UploadImageAjax(UploadingFile image)
        {
            var result = new ResponseViewModel();

            try
            {
                var vechile = _ownerService.GetVehicle(int.Parse(User.Identity.GetUserId()), image.VehicleId);
                if (vechile == null) { throw new CatchableException("Vechicle not found."); }

                if (image.File == null || image.File.ContentLength <= 0) { throw new CatchableException("Image content can not be empty."); }

                var supportedTypes = new[] { ".jpg", ".jpeg", ".png" };
                if (!supportedTypes.Contains(System.IO.Path.GetExtension(image.File.FileName)))
                {
                    throw new CatchableException( string.Format("Image type not supported. Only folowing types are supported ({0})", string.Join(",", supportedTypes)));
                }

                var imagePath = string.Format(VEHICLE_IMAGE, image.VehicleId);
                imagePath = Server.MapPath(imagePath);

                if(!System.IO.Directory.Exists(imagePath)){ System.IO.Directory.CreateDirectory(imagePath);}
                imagePath = System.IO.Path.Combine(imagePath, image.File.FileName);
                image.File.SaveAs(imagePath);

                var model = new VehicleImage() { VehicleId = image.VehicleId, IsDefaultImage = false, ImagePath = image.File.FileName };
                if(!_ownerService.AddImage(model))
                {
                    throw new CatchableException("Image saving failed.");
                }
            }
            catch (CatchableException exp)
            {
                result.Status = "Error";
                result.Message = exp.Message;
            }

            return Json(result);
        }
Пример #2
0
 public bool AddImage(VehicleImage model)
 {
     if (model == null) { return false; }
     _repository.Add(model);
     _unitOfWork.Commit();
     return true;
 }