private Models.PropertyPicturesModel GetPictures(long propertyId) { Models.PropertyPicturesModel pictures = new Models.PropertyPicturesModel(); CLayer.Property data = BLayer.Property.Get(propertyId); if (data != null) { pictures.PropertyId = propertyId; pictures.Pictures = BLayer.PropertyFiles.GetAll(propertyId);//, CLayer.PropertyFiles.FileTypes.images } else { pictures.PropertyId = -1; pictures.Pictures = new List <CLayer.PropertyFiles>(); } return(pictures); }
public ActionResult PictureSave(Models.PropertyPicturesModel data) { try { if (ModelState.IsValid) {//.....ImageFile resize save new #region Save string FileNamePart = data.PropertyId.ToString() + DateTime.Now.ToString("ddMMyyyyHHmmss"); CLayer.PropertyFiles prprty = new CLayer.PropertyFiles() { PropertyId = data.PropertyId, FileId = data.FileId, FileName = FileNamePart + System.IO.Path.GetFileNameWithoutExtension(data.photo.FileName) + ".jpg" }; if (data.photo != null && data.photo.ContentLength > 0) { int Sizevalue = Convert.ToInt32(ConfigurationManager.AppSettings.Get("FileUploadSizeInMB")); int MaxContentLength = 1024 * 1024 * Sizevalue; //3 MB string[] AllowedFileExtensions = new string[] { ".jpg", ".gif", ".png", ".jpeg" }; if (!AllowedFileExtensions.Contains(data.photo.FileName.Substring(data.photo.FileName.LastIndexOf('.')))) { ModelState.AddModelError(string.Empty, "Please use files of type: " + string.Join(", ", AllowedFileExtensions)); } else if (data.photo.ContentLength > MaxContentLength) { ModelState.AddModelError(string.Empty, "Your file is too large, maximum allowed size is: " + MaxContentLength + " MB"); } else { var fileName = FileNamePart + System.IO.Path.GetFileName(data.photo.FileName); //uploaded file from UI if (!System.IO.Directory.Exists(Server.MapPath("~/Files/Temp/"))) { System.IO.Directory.CreateDirectory(Server.MapPath("~/Files/Temp/")); } var path = System.IO.Path.Combine(Server.MapPath("~/Files/Temp/"), fileName); data.photo.SaveAs(path); int maxHeight = Convert.ToInt32(ConfigurationManager.AppSettings.Get("MaxImgHeight")); Image tempimage = Image.FromFile(path); Image resized = Common.Utils.ScaleImage(tempimage, maxHeight); // var = prprty.FileName;// DateTime.Now.ToString("ddMMyyyyHHmmss") + System.IO.Path.GetFileName(data.photo.FileName); if (!System.IO.Directory.Exists(Server.MapPath("~/Files/Property/" + data.PropertyId.ToString() + "/"))) { System.IO.Directory.CreateDirectory(Server.MapPath("~/Files/Property/" + data.PropertyId.ToString() + "/")); } var path2 = System.IO.Path.Combine(Server.MapPath("~/Files/Property/" + data.PropertyId.ToString() + "/"), prprty.FileName); resized.Save(path2, System.Drawing.Imaging.ImageFormat.Jpeg); try { resized.Dispose(); tempimage.Dispose(); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } } catch { } BLayer.PropertyFiles.Save(prprty); ModelState.Clear(); } } #endregion Models.PropertyModel property = GetDetails(data.PropertyId); property.ActiveTab = "gallery"; return(View("Index", property)); } return(Redirect("~/ErrorPage")); } catch (Exception ex) { Common.LogHandler.HandleError(ex); return(Redirect("~/ErrorPage")); } }