示例#1
0
        public ActionResult Edit(int id, ContentItem model, HttpPostedFileBase file)
        {
            try
            {
                var contentItem = _context.PostItems.First(p => p.Id == id);

                contentItem.Text = model.Text == null ? "" : HttpUtility.HtmlDecode(model.Text);

                if (file != null)
                {
                    if (!string.IsNullOrEmpty(contentItem.ImageSrc))
                    {
                        ImageHelper.DeleteImage(contentItem.ImageSrc);
                    }

                    string fileName = IOHelper.GetUniqueFileName("~/Content/Images", file.FileName);
                    string filePath = Server.MapPath("~/Content/Images");
                    filePath = Path.Combine(filePath, fileName);
                    //file.SaveAs(filePath);
                    GraphicsHelper.SaveOriginalImageWithDefinedDimentions(filePath, fileName, file, 556, 0, ScaleMode.FixedWidth);
                    contentItem.ImageSrc = fileName;

                }

                _context.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
示例#2
0
        public ActionResult Create(ContentItem model, HttpPostedFileBase file)
        {
            try
            {
                var contentItem = new ContentItem
                {
                    Text = model.Text == null ? "" : HttpUtility.HtmlDecode(model.Text),
                };

                if (file != null)
                {
                    string fileName = IOHelper.GetUniqueFileName("~/Content/Images", file.FileName);
                    string filePath = Server.MapPath("~/Content/Images");
                    filePath = Path.Combine(filePath, fileName);
                    //file.SaveAs(filePath);
                    GraphicsHelper.SaveOriginalImageWithDefinedDimentions(filePath, fileName, file, 556, 0, ScaleMode.FixedWidth);
                    contentItem.ImageSrc = fileName;
                }

                _context.ContentItems.Add(contentItem);
                _context.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }