private string ProcessUploadedFile(BlogEditViewModel model, Blog blog) { string uniqueFileName = null; if (model.Images != null && model.Images.Count > 0) { foreach (IFormFile photo in model.Images) { string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "userdata/blogs"); uniqueFileName = Guid.NewGuid().ToString() + "_" + photo.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { photo.CopyTo(fileStream); } Image image = new Image(); image.ImageId = Guid.NewGuid(); image.ImagePath = uniqueFileName; BlogImages newImage = new BlogImages(); newImage.Blog = blog; newImage.Image = image; //context.BlogImages.Add(newImage); context.SaveChanges(); } } return(uniqueFileName); }
public void UpdateBlogIMages(BlogImages updateBlogimages, BlogImages blogimages) { updateBlogimages.Status = blogimages.Status; updateBlogimages.BlogId = blogimages.BlogId; updateBlogimages.Image = blogimages.Image; _context.SaveChanges(); }
public ActionResult Create(Blogs blogs, HttpPostedFileBase image) { if (ModelState.IsValid) { blogs.State = true; context.Blogs.Add(blogs); context.SaveChanges(); if (image != null) { WebImage img = new WebImage(image.InputStream); FileInfo imgInfo = new FileInfo(image.FileName); string imgPath = Guid.NewGuid().ToString() + imgInfo.Extension; img.Resize(1200, 675, false, false); img.Save(Server.MapPath("~/Content/MyWebSite/img/BlogImages/" + imgPath)); BlogImages blogImages = new BlogImages(); var lastBlog = context.Blogs.OrderByDescending(x => x.Id).FirstOrDefault(); blogImages.BlogId = lastBlog.Id; blogImages.State = true; blogImages.ImagePath = "/Content/MyWebSite/img/BlogImages/" + imgPath; context.BlogImages.Add(blogImages); context.SaveChanges(); } return(RedirectToAction("Index")); } return(View()); }
public ActionResult Create(Blog blog) { if (ModelState.IsValid) { Blog Blog = new Blog(); Blog.Title = blog.Title; Blog.Quote = blog.Quote; Blog.Content = blog.Content; Blog.Read = 0; Blog.CategoryId = blog.CategoryId; Blog.AdminId = (int)Session["LoginnerId"]; Blog.CreateDate = DateTime.Now; db.Blog.Add(Blog); db.SaveChanges(); foreach (HttpPostedFileBase image in blog.ImageFile) { string imgName = DateTime.Now.ToString("ddMMyyyyHHmmssfff") + image.FileName; string imgPath = Path.Combine(Server.MapPath("~/Uploads/Blog/"), imgName); image.SaveAs(imgPath); BlogImages img = new BlogImages(); img.Name = imgName; img.BlogId = Blog.Id; db.BlogImages.Add(img); db.SaveChanges(); } foreach (var item in blog.TagId) { Tag tag = new Tag(); tag.BlogId = Blog.Id; tag.BlogTagsId = item; db.Tag.Add(tag); db.SaveChanges(); } return(RedirectToAction("Index")); } ViewBag.Category = db.BlogCategory.ToList(); return(View(blog)); }
public ActionResult AddImage(Blogs blogs, HttpPostedFileBase image) { if (image != null) { WebImage img = new WebImage(image.InputStream); FileInfo imgInfo = new FileInfo(image.FileName); string imgPath = Guid.NewGuid().ToString() + imgInfo.Extension; img.Resize(1200, 675, false, false); img.Save(Server.MapPath("~/Content/MyWebSite/img/BlogImages/" + imgPath)); BlogImages blogImages = new BlogImages(); blogImages.BlogId = blogs.Id; blogImages.ImagePath = "/Content/MyWebSite/img/BlogImages/" + imgPath; blogImages.State = true; context.BlogImages.Add(blogImages); context.SaveChanges(); return(RedirectToAction("Index")); } return(HttpNotFound()); }
private string ProcessUploadedFile(BlogEditViewModel model) { string uniqueFileName = null; if (model.Images != null && model.Images.Count > 0) { foreach (IFormFile photo in model.Images) { string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "userdata/blogs"); uniqueFileName = Guid.NewGuid().ToString() + "_" + photo.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { photo.CopyTo(fileStream); } var currentBlog = context.Blogs.AsNoTracking().Include(p => p.BlogCategories).Single(p => p.BlogId == model.Blog.BlogId); Image image = new Image(); image.ImageId = Guid.NewGuid(); image.ImagePath = uniqueFileName; BlogImages newImage = new BlogImages(); newImage.Blog = currentBlog; newImage.Image = image; using (var a = new AppDbContext(contextOptions)) { a.Blogs.Include(p => p.BlogImages).Single(p => p.BlogId == model.Blog.BlogId).BlogImages.Add(newImage); a.SaveChanges(); } } } return(uniqueFileName); }
public ActionResult Edit(Blog blog) { Blog blog1 = db.Blog.Find(blog.Id); if (ModelState.IsValid) { blog1.Title = blog.Title; blog1.CategoryId = blog.CategoryId; blog1.Quote = blog.Quote; blog1.Content = blog.Content; db.Entry(blog1).State = EntityState.Modified; //foreach (var item in blog.TagId) //{ // List<Tag> tags = db.Tag.Where(p => p.BlogId == blog.Id).ToList(); // foreach(var tag in tags) // { // tag.BlogTagsId = tag.Id; // db.Entry(tag).State = EntityState.Modified; // db.SaveChanges(); // } //} if (blog.ImageFile.Length > 1) { List <BlogImages> imgs = db.BlogImages.Where(p => p.BlogId == blog.Id).ToList(); foreach (var img in imgs) { if (img != null) { string oldPath = Path.Combine(Server.MapPath("~/Uploads/Blog/"), img.Name); System.IO.File.Delete(oldPath); db.BlogImages.Remove(img); db.SaveChanges(); } } foreach (HttpPostedFileBase image in blog.ImageFile) { if (image != null) { string imgName = DateTime.Now.ToString("ddMMyyyyHHmmssfff") + image.FileName; string imgPath = Path.Combine(Server.MapPath("~/Uploads/Blog/"), imgName); image.SaveAs(imgPath); BlogImages img = new BlogImages(); img.Name = imgName; img.BlogId = blog1.Id; db.BlogImages.Add(img); db.SaveChanges(); } } } db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.Product = db.Product.ToList(); ViewBag.Color = db.Colors.ToList(); ViewBag.Size = db.Size.ToList(); return(View(blog)); }
public void AddImages(Images[] images) { BlogImages.AddRange(images); }
public void RemoveBlogIMages(BlogImages blogimage) { _context.BlogImages.Remove(blogimage); _context.SaveChanges(); }
public BlogImages AddBlogIMages(BlogImages blogImages) { _context.BlogImages.Add(blogImages); _context.SaveChanges(); return(blogImages); }
protected override void Seed(DatabaseContext context) { for (int i = 0; i < 10; i++) { Users user = new Users(); user.Name = FakeData.NameData.GetFirstName(); user.Surname = FakeData.NameData.GetSurname(); user.Email = FakeData.NetworkData.GetEmail(); user.Adress = FakeData.PlaceData.GetAddress(); user.Phone = "553 510 1122"; user.Password = "******";//123 user.UserName = FakeData.NameData.GetCompanyName(); user.BannedStatus = FakeData.BooleanData.GetBoolean(); user.AccountStatus = FakeData.BooleanData.GetBoolean(); context.Users.Add(user); } context.SaveChanges(); for (int i = 0; i < 10; i++) { Categories categories = new Categories(); categories.CategoriName = FakeData.PlaceData.GetCountry(); context.Categories.Add(categories); } context.SaveChanges(); List <Users> users = context.Users.ToList(); List <Categories> kategori = context.Categories.ToList(); for (int i = 0; i < 3; i++) { AdminList admin = new AdminList(); admin.Users = users[i]; admin.AuthDegree = 2; admin.AuthName = "Admin"; context.AdminList.Add(admin); } foreach (Users user in users) { for (int i = 0; i < FakeData.NumberData.GetNumber(1, 5); i++) { Blogs blog = new Blogs(); try { blog.Users = user; blog.Categories = kategori[i]; blog.Title = FakeData.TextData.GetSentence(); blog.Text = FakeData.TextData.GetSentences(2); blog.DefaultImage = "bc06537e49814c258bd0b6d738a1e792.jpg"; blog.AddDate = FakeData.DateTimeData.GetDatetime(); blog.UpdateDate = FakeData.DateTimeData.GetDatetime(); blog.NumOfLikes = FakeData.NumberData.GetNumber(1, 10000); blog.BannedStatus = FakeData.BooleanData.GetBoolean(); blog.BlogStatus = FakeData.BooleanData.GetBoolean(); context.Blogs.Add(blog); context.SaveChanges(); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors)//faydalı bir kod 1 saat sonunda sorunu bulmamı sağladı :D { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } for (int t = 0; t < 5; t++) { BlogImages blogImages = new BlogImages(); blogImages.Blogs = blog; blogImages.ImagesPath = "bc06537e49814c258bd0b6d738a1e792.jpg"; context.BlogImages.Add(blogImages); Comments comments = new Comments(); comments.Blogs = blog; comments.Users = user; comments.CommentDate = DateTime.Now; comments.Text = FakeData.TextData.GetSentence(); context.Comments.Add(comments); } } } context.SaveChanges(); }