Exemple #1
0
        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);
        }
Exemple #2
0
 public bool AddDoc(VehicleDocument model)
 {
     if (model == null) { return false; }
     _repository.Add(model);
     _unitOfWork.Commit();
     return true;
 }