public ActionResult UploadDocumentAjax(UploadingFile document) { var result = new ResponseViewModel(); try { var vechile = _ownerService.GetVehicle(int.Parse(User.Identity.GetUserId()), document.VehicleId); if (vechile == null) { throw new CatchableException("Vechicle not found."); } if (document.File == null || document.File.ContentLength <= 0) { throw new CatchableException("Image content can not be empty."); } var supportedTypes = new[] {".pdf",}; if (!supportedTypes.Contains(System.IO.Path.GetExtension(document.File.FileName))) { throw new CatchableException(string.Format("Image type not supported. Only folowing types are supported ({0})", string.Join(",", supportedTypes))); } var filepath = string.Format(VEHICLE_DOCUMENT, document.VehicleId); filepath = Server.MapPath(filepath); if (!System.IO.Directory.Exists(filepath)) { System.IO.Directory.CreateDirectory(filepath); } filepath = System.IO.Path.Combine(filepath, document.File.FileName); document.File.SaveAs(filepath); System.IO.Path.GetFileNameWithoutExtension(filepath); var model = new VehicleDocument() { VehicleId = document.VehicleId, Path = document.File.FileName, FileName = System.IO.Path.GetFileNameWithoutExtension(filepath) }; if (!_ownerService.AddDoc(model)) { throw new CatchableException("Document saving failed."); } } catch (CatchableException exp) { result.Status = "Error"; result.Message = exp.Message; } return Json(result); }
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); }