public ActionResult Addbook(Book book, HttpPostedFileBase picture, int categoryId) { if ( ModelState.IsValid ) { if ( picture != null ) { if ( picture.ContentType == "image/jpeg" || picture.ContentType == "image/png" ) { Image image = Image.FromStream(picture.InputStream); if ( image.Height > 200 || image.Width > 200 ) { Image small = Helper.ImageHelper.ScaleImage(image, 200, 200); Bitmap b = new Bitmap(small); Guid guid = Guid.NewGuid(); string imageName = guid.ToString() + ".jpg"; b.Save(Server.MapPath("~/uploads/aticleImage/" + imageName), ImageFormat.Jpeg); b.Save(Server.MapPath("~/uploads/articleImage/" + imageName), ImageFormat.Jpeg); small.Dispose(); b.Dispose(); book.ImageName = imageName; } else { picture.SaveAs(Server.MapPath("~/uploads/Article/" + picture.FileName)); } } } BookCategoryDao bookCategoryDao = new BookCategoryDao(); BookCategory bookCategory = bookCategoryDao.GetById(categoryId); book.Category = bookCategory; book.PosteDate = DateTime.Now; BookDao bookDao = new BookDao(); bookDao.Create(book); TempData["message-success"] = "Článek byl přidán"; } else { return View("Create", book); } return RedirectToAction("Index"); }
public ActionResult UpdateBook(Book book, HttpPostedFileBase picture, int categoryId) { try { BookDao bookDao = new BookDao(); BookCategoryDao bookCategoryDao = new BookCategoryDao(); BookCategory bookCategory = bookCategoryDao.GetById(categoryId); User user = new UserDao().GetByLogin(User.Identity.Name); book.Category = bookCategory; if ( picture != null ) { if ( picture.ContentType == "image/jpeg" || picture.ContentType == "image/png" ) { Image image = Image.FromStream(picture.InputStream); Guid guid = Guid.NewGuid(); string imageName = guid.ToString() + ".jpg"; if ( image.Height > 200 || image.Width > 200 ) { Image small = Helper.ImageHelper.ScaleImage(image, 200, 200); Bitmap b = new Bitmap(small); b.Save(Server.MapPath("~/uploads/articleImage/" + imageName), ImageFormat.Jpeg); small.Dispose(); b.Dispose(); } else { picture.SaveAs(Server.MapPath("~/uploads/articleImage/" + picture.FileName)); } System.IO.File.Delete(Server.MapPath("~/uploads/articleImage/" + book.ImageName)); book.ImageName = imageName; }; } bookDao.Update(book); TempData["message-success"] = "Článek " + book.Title + " byl upravena."; } catch ( Exception ) { throw; } return RedirectToAction("Index"); }