public async Task <IActionResult> Create(BlogViewModel blog) { if (ModelState.IsValid) { var blogForSave = _mapper.Map <BlogViewModel, Blog>(blog); var blogFromDb = _blogData.AddBlog(blogForSave); var fileExt = blog.Wallpaper.FileName.Split('.').Last(); string path = $"{Path.DirectorySeparatorChar}images{Path.DirectorySeparatorChar}" + $"blogWallpapers{Path.DirectorySeparatorChar}" + blogFromDb.Id + $".{fileExt}"; using (var fileStream = new FileStream(_env.WebRootPath + path, FileMode.Create)) { await blog.Wallpaper.CopyToAsync(fileStream); } blogFromDb.ImageUrl = "/images/blogWallpapers/" + blogFromDb.Id + $".{fileExt}"; blogFromDb.ReleaseTime = DateTime.Now; blogFromDb.ImageLocalUrl = _env.WebRootPath + path; blogFromDb.Author = await _userManager.GetUserAsync(User); _blogData.UpdateBlogs(blogFromDb); return(RedirectToAction("Index", "Profile")); } return(View()); }
public IActionResult OnPost() { var Username = HttpContext.Session.GetString("username"); if (string.IsNullOrEmpty(Username)) { return(RedirectToPage("/Index")); } SingleUserData = userData.GetByUserName(Username); writeBlog.Users = SingleUserData; ; if (BlogPicture != null) { writeBlog.BlogImage = UploadedPicture(BlogPicture, SingleUserData.UserName); } var newBlog = new Blogs() { Users = writeBlog.Users, BlogTitle = writeBlog.BlogTitle, BlogDescription = writeBlog.BlogDescription, Blogcontent = writeBlog.Blogcontent, BlogDate = DateTime.Now.Date, BlogImage = writeBlog.BlogImage }; bool res = blogData.AddBlog(newBlog); if (res) { blogData.Commit(); return(RedirectToPage("/User/Dashboard", new { urlname = Username })); } return(Page()); }