public async Task <JsonResult> AddBlog(AddBlog blog) { var result = new SuccessResult(); try { using (var db = new TryNetCoreContext()) { var newblog = new Blog(); newblog.BlogName = blog.blogname; newblog.BlogAuthor = blog.blogauthorname; newblog.BlogCategoryName = blog.blogcategoryname; newblog.BlogPostDate = DateTime.Now; var blogcount = db.Blog.Count(); newblog.BlogRouteUrl = FriendlyUrl.FriendlyUrlMethod(blog.blogname + "-" + blogcount); newblog.BlogImagePath = await TryNetCore.Utils.FileUpload.ImageUpload(host.ContentRootPath, blog.blogimage, null); var blogtags = blog.blogtags.Split(','); for (int i = 0; i < blogtags.Length; i++) { newblog.BlogTags.Add(new BlogTags() { BlogTagName = blogtags[i] }); } // ------------------------- // // BLOG CONTENT AGİLİTY PACK // HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(blog.blogcontent); var htmlimages = doc.DocumentNode.SelectNodes("//img"); if (htmlimages != null) { foreach (var htmlimage in htmlimages) { var srcattribute = htmlimage.GetAttributeValue("src", "defaultvalue"); var parseattributearray = srcattribute.Split(','); var parsebase64 = parseattributearray[1]; byte[] imageBytes = Convert.FromBase64String(parsebase64); using (var ms = new MemoryStream()) { ms.Write(imageBytes, 0, imageBytes.Length); IFormFile file = new FormFile(ms, 0, imageBytes.Length, blog.blogimage.Name, blog.blogimage.FileName); var blogimagepath = await TryNetCore.Utils.FileUpload.ImageUpload(host.ContentRootPath, file, null); newblog.BlogImages.Add(new BlogImages() { ImagePath = blogimagepath }); var updatesrcattribute = blogimagepath; htmlimage.SetAttributeValue("src", updatesrcattribute); } } } newblog.BlogContent = doc.DocumentNode.InnerHtml; await db.Blog.AddAsync(newblog); await db.SaveChangesAsync(); } result.isSuccess = true; result.Message = "Başarılı"; return(Json(result)); } catch (Exception e) { result.isSuccess = false; result.Message = "Hata"; return(Json(result)); } }
public async Task <IActionResult> UpdateBlog(AddBlog blog, int blogid) { var result = new SuccessResult(); try { using (var db = new TryNetCoreContext()) { var updateblog = db.Blog.Where(i => i.Id == blogid) .Include(i => i.BlogTags) .Include(i => i.BlogImages) .FirstOrDefault(); updateblog.BlogName = blog.blogname; updateblog.BlogAuthor = blog.blogauthorname; updateblog.BlogCategoryName = blog.blogcategoryname; var blogcount = db.Blog.Count(); updateblog.BlogRouteUrl = FriendlyUrl.FriendlyUrlMethod(blog.blogname + "-" + blogcount); if (blog.blogimage != null && blog.blogimage.Length > 0) { updateblog.BlogImagePath = await TryNetCore.Utils.FileUpload.ImageUpload(host.ContentRootPath, blog.blogimage, updateblog.BlogImagePath); } updateblog.BlogTags.Clear(); var blogtags = blog.blogtags.Split(','); for (int i = 0; i < blogtags.Length; i++) { updateblog.BlogTags.Add(new BlogTags() { BlogTagName = blogtags[i] }); } // ------------------------- // // BLOG CONTENT AGİLİTY PACK // HtmlDocument deletedoc = new HtmlDocument(); deletedoc.LoadHtml(updateblog.BlogContent); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(blog.blogcontent); var htmlimages = doc.DocumentNode.SelectNodes("//img"); List <String> deleteimages = new List <String>(); if (htmlimages != null) { foreach (var htmlimage in htmlimages) { var srcattribute = htmlimage.GetAttributeValue("src", "defaultvalue"); var parseattributearray = srcattribute.Split(','); if (parseattributearray.Length > 1) { var parsebase64 = parseattributearray[1]; byte[] imageBytes = Convert.FromBase64String(parsebase64); using (var ms = new MemoryStream()) { ms.Write(imageBytes, 0, imageBytes.Length); IFormFile file = new FormFile(ms, 0, imageBytes.Length, "denemeisim", "denemeisim.jpg"); var blogimagepath = await TryNetCore.Utils.FileUpload.ImageUpload(host.ContentRootPath, file, null); updateblog.BlogImages.Add(new BlogImages() { ImagePath = blogimagepath }); var updatesrcattribute = blogimagepath; htmlimage.SetAttributeValue("src", updatesrcattribute); } } else { deleteimages.Add(parseattributearray[0]); } } } updateblog.BlogContent = doc.DocumentNode.InnerHtml; await db.SaveChangesAsync(); } result.isSuccess = true; result.Message = "Başarılı"; return(Json(result)); } catch (Exception e) { var a = e.Message; result.isSuccess = false; result.Message = "Hata"; return(Json(result)); } }