public ActionResult SaveUserPhoto(HttpPostedFileBase file, string title) { int membershipId = -1; if (Auth.IsAuthenticated(Request, User, out membershipId)) //if (IsAuthenticated(out membershipId)) { ImageProcessing imgHelper = new ImageProcessing(); using (System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream)) { //Initialise the size of the array byte[] f = new byte[file.InputStream.Length]; //Create a new BinaryReader and set the InputStream for the Images InputStream to the beginning, as we create the img using a stream. var reader = new BinaryReader(file.InputStream); file.InputStream.Seek(0, SeekOrigin.Begin); //Load the image binary. f = reader.ReadBytes((int)file.InputStream.Length); f = imgHelper.ResizeImage(f, new System.Drawing.Size(120, 90)); bool newRecord = true; var existUserData = (from s in _db.UserPhotos where s.MembershipId == membershipId select s).SingleOrDefault<UserPhoto>(); if (existUserData == null) { existUserData = new UserPhoto() { img=f, MembershipId=membershipId }; } else { newRecord = false; existUserData.img = f; } existUserData.MembershipId = membershipId; existUserData.Title = Server.HtmlEncode(title); if (newRecord) _db.UserPhotos.Add(existUserData); else { _db.UserPhotos.Attach(existUserData); _db.Entry(existUserData).State = EntityState.Modified; } _db.SaveChanges(); } return RedirectToAction("Index"); } return RedirectToAction("index", "home"); }
public ActionResult UploadImage(int objId, HttpPostedFileBase file = null) { // Verify that the user selected a file if (file != null && file.ContentLength > 0) { // extract only the fielname // var fileName = Path.GetFileName(file.FileName); // store the file inside ~/App_Data/uploads folder // var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName); // file.SaveAs(path); var propObj = (from s in _db.PropertyObjects .Include("City") .Include("CityDistrict") .Include("PropertyType") .Include("PropertyAction") .Include("BuildingTypeName") .Include("Currency") .Include("PriceForTypeName") .Include("UserOwner") .Include("WCType") .Include("CommercialPropertyType") .Include("ServiceType") .Include("Periods") where s.Id == objId select s).SingleOrDefault<PropertyObject>(); ImageProcessing imgHelper = new ImageProcessing(); using (System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream)) { //Initialise the size of the array byte[] f = new byte[file.InputStream.Length]; //Create a new BinaryReader and set the InputStream for the Images InputStream to the beginning, as we create the img using a stream. var reader = new BinaryReader(file.InputStream); file.InputStream.Seek(0, SeekOrigin.Begin); //Load the image binary. f = reader.ReadBytes((int)file.InputStream.Length); //Create a new image to be added to the database if (img.Width > 512) f = imgHelper.ResizeImage(f, new System.Drawing.Size(512, 389)); ObjectImages objImages = new ObjectImages() { Content_Type = file.ContentType, FileName = file.FileName, FileSize = file.ContentLength, Height = (img.Width > 512 ? 512 : img.Height), Width = (img.Width > 512 ? 512 : img.Width), Image = f, ImagePreview = imgHelper.ResizeImage(f, new System.Drawing.Size(120, 90)), PropertyObject = propObj, IsDeleted = false }; _db.ObjectImages.Add(objImages); _db.Entry(objImages.PropertyObject).State = System.Data.EntityState.Unchanged; _db.SaveChanges(); } var countImages = (from s in _db.ObjectImages where s.PropertyObject.Id == propObj.Id && s.IsDeleted == false select 1).Count<int>(); propObj.CountPhotos = countImages; _db.PropertyObjects.Attach(propObj); _db.Entry(propObj).Property(x => x.CountPhotos).IsModified = true; _db.SaveChanges(); } PropertyPreview result = new PropertyPreview(); var smallImageIds = from s in _db.ObjectImages.AsQueryable() where s.PropertyObject.Id == objId && s.IsDeleted == false select s.Id; foreach (var item in smallImageIds) { if (result.ImgPreviewIds == null) result.ImgPreviewIds = new List<int>(); result.ImgPreviewIds.Add(item); } result.PropertObject = (from s in _db.PropertyObjects where s.Id == objId select s).Single<PropertyObject>(); return RedirectToAction("GetImageUploadPreview", new { objId = objId }); //ViewBag.ObjId = objId; //return View(result); // redirect back to the index action to show the form once again }