public ActionResult newimages(HttpPostedFileBase file, string size, string Product, string PharmaF, string Category, string Division, string Edition) { try { CountriesUsers pp = (CountriesUsers)Session["CountriesUsers"]; int ApplicationId = pp.ApplicationId; int UsersId = pp.userId; ProductImages ProductImages = new Models.ProductImages(); int SizeId = int.Parse(size); int ProductId = int.Parse(Product); int PharmaFormId = int.Parse(PharmaF); int CategoryId = int.Parse(Category); int DivisionId = int.Parse(Division); int EditionId = int.Parse(Edition); String FileName = Path.GetFileName(file.FileName); String extention = Path.GetExtension(file.FileName); FileName = FileName.Replace(extention, ""); var prods = (from p in db.Products where p.ProductId == ProductId select p).ToList(); foreach (Products _prods in prods) { FileName = _prods.ProductName.Trim().ToUpper(); } FileName = ReplacesImageNames.replaces(FileName); FileName = FileName + extention; var img = (from _ims in db.ImageSizes where _ims.ImageSizeId == SizeId select _ims).ToList(); foreach (ImageSizes _img in img) { var root = Path.Combine(Server.MapPath("~/App_Data/uploads/ProductShots"), _img.Size); var path = Path.Combine(System.Configuration.ConfigurationManager.AppSettings["Path3"], _img.Size); if (System.IO.Directory.Exists(root)) { //path = path + "\\" + FileName; root = root + "\\" + FileName; //file.SaveAs(path); file.SaveAs(root); } else { DirectoryInfo Dir = Directory.CreateDirectory(root); root = Path.Combine(root, FileName); file.SaveAs(root); if (System.IO.Directory.Exists(root)) { root = root + "\\" + FileName; file.SaveAs(root); } //else //{ // DirectoryInfo Dirs = Directory.CreateDirectory(root); // root = Path.Combine(root, FileName); // file.SaveAs(root); //} } } var edp = (from _edp in db.ProductCategories where _edp.CategoryId == CategoryId && _edp.DivisionId == DivisionId && _edp.PharmaFormId == PharmaFormId && _edp.ProductId == ProductId select _edp).ToList(); if (edp.LongCount() > 0) { var eps = (from _eps in db.ProductImages where _eps.CategoryId == CategoryId && _eps.DivisionId == DivisionId && _eps.PharmaFormId == PharmaFormId && _eps.ProductId == ProductId && _eps.ProductShot == FileName select _eps).ToList(); if (eps.LongCount() == 0) { ProductImages.Active = true; ProductImages.CategoryId = CategoryId; ProductImages.DivisionId = DivisionId; ProductImages.PharmaFormId = PharmaFormId; ProductImages.ProductId = ProductId; ProductImages.ProductShot = FileName; ProductImages.BaseURL = null; db.ProductImages.Add(ProductImages); db.SaveChanges(); var epss = (from _eps in db.ProductImages where _eps.CategoryId == CategoryId && _eps.DivisionId == DivisionId && _eps.PharmaFormId == PharmaFormId && _eps.ProductId == ProductId && _eps.ProductShot == FileName select _eps).ToList(); int ProductImageSizeId = 0; foreach (ProductImages _epss in epss) { ProductImageSizeId = _epss.ProductImageId; var pis = (from _pis in db.ProductImageSizes where _pis.ImageSizeId == SizeId && _pis.ProductImageId == _epss.ProductImageId select _pis).ToList(); if (pis.LongCount() == 0) { ProductImageSizes ProductImageSizes = new ProductImageSizes(); ProductImageSizes.ImageSizeId = Convert.ToByte(SizeId); ProductImageSizes.ProductImageId = _epss.ProductImageId; db.ProductImageSizes.Add(ProductImageSizes); db.SaveChanges(); ActivityLog._insertProductImageSizes(ProductImageSizeId, SizeId, ApplicationId, UsersId); } ActivityLog._insertproductimages(ProductId, PharmaFormId, DivisionId, CategoryId, ProductImageSizeId, FileName, ApplicationId, UsersId); } } else { var epss = (from _eps in db.ProductImages where _eps.CategoryId == CategoryId && _eps.DivisionId == DivisionId && _eps.PharmaFormId == PharmaFormId && _eps.ProductId == ProductId && _eps.ProductShot == FileName select _eps).ToList(); int ProductImageSizeId = 0; foreach (ProductImages _epss in epss) { ProductImageSizeId = _epss.ProductImageId; var pis = (from _pis in db.ProductImageSizes where _pis.ImageSizeId == SizeId && _pis.ProductImageId == _epss.ProductImageId select _pis).ToList(); if (pis.LongCount() == 0) { ProductImageSizes ProductImageSizes = new ProductImageSizes(); ProductImageSizes.ImageSizeId = Convert.ToByte(SizeId); ProductImageSizes.ProductImageId = _epss.ProductImageId; db.ProductImageSizes.Add(ProductImageSizes); db.SaveChanges(); ActivityLog._insertProductImageSizes(ProductImageSizeId, SizeId, ApplicationId, UsersId); } } ProductImages.ProductShot = FileName; db.SaveChanges(); ActivityLog._updateproductimages(ProductId, PharmaFormId, DivisionId, CategoryId, ProductImageSizeId, FileName, ApplicationId, UsersId); } } return(Json(true, JsonRequestBehavior.AllowGet)); } catch (Exception e) { String msg = e.Message; return(Json(false, JsonRequestBehavior.AllowGet)); } }