Ejemplo n.º 1
0
        public ActionResult Create(Article model, HttpPostedFileBase file, IEnumerable<HttpPostedFileBase> files)
        {
            try
            {
                var article = new Article
                {
                    Title = model.Title,
                    TitleEn = model.TitleEn,
                    TitleUa = model.TitleUa,
                    Date = model.Date,
                    TitlePosition = model.TitlePosition,
                    VideoSrc = model.VideoSrc
                };

                article.Text = model.Text == null ? "" : HttpUtility.HtmlDecode(model.Text);
                article.TextEn = model.TextEn == null ? "" : HttpUtility.HtmlDecode(model.TextEn);
                article.TextUa = model.TextUa == null ? "" : HttpUtility.HtmlDecode(model.TextUa);

                if (file != null)
                {
                    string fileName = IOHelper.GetUniqueFileName("~/Content/Images", file.FileName);
                    string filePath = Server.MapPath("~/Content/Images");
                    filePath = Path.Combine(filePath, fileName);
                    GraphicsHelper.SaveOriginalImageWithDefinedDimentions(filePath, fileName, file, 340, 290, ScaleMode.Crop);
                    article.ImageSrc = fileName;
                }

                foreach (var f in files)
                {
                    if (f == null) continue;
                    string fileName = IOHelper.GetUniqueFileName("~/Content/Images", f.FileName);
                    string filePath = Server.MapPath("~/Content/Images");

                    filePath = Path.Combine(filePath, fileName);

                    // h: 283
                    // w: 400
                    GraphicsHelper.SaveOriginalImageWithDefinedDimentions(filePath, fileName, f, 400, 283, ScaleMode.Crop);

                    var ai = new ArticleImage
                    {
                        ImageSrc = fileName
                    };

                    article.ArticleImages.Add(ai);
                }

                _context.Articles.Add(article);
                _context.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
Ejemplo n.º 2
0
        public ActionResult Edit(Article model, int id, HttpPostedFileBase file, IEnumerable<HttpPostedFileBase> files)
        {
            try
            {
                var article = _context.Articles.First(a => a.Id == id);

                TryUpdateModel(article, new[] { "Title", "TitleEn", "TitleUa", "Date", "VideoSrc", "TitlePosition" });
                
                article.Text = model.Text == null ? "" : HttpUtility.HtmlDecode(model.Text);
                article.TextEn = model.TextEn == null ? "" : HttpUtility.HtmlDecode(model.TextEn);
                article.TextUa = model.TextUa == null ? "" : HttpUtility.HtmlDecode(model.TextUa);

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

                    string fileName = IOHelper.GetUniqueFileName("~/Content/Images", file.FileName);
                    string filePath = Server.MapPath("~/Content/Images");
                    filePath = Path.Combine(filePath, fileName);
                    GraphicsHelper.SaveOriginalImageWithDefinedDimentions(filePath, fileName, file, 340, 290, ScaleMode.Crop);
                    article.ImageSrc = fileName;

                }

                foreach (var f in files)
                {
                    if (f == null) continue;
                    string fileName = IOHelper.GetUniqueFileName("~/Content/Images", f.FileName);
                    string filePath = Server.MapPath("~/Content/Images");

                    filePath = Path.Combine(filePath, fileName);
                    //GraphicsHelper.SaveOriginalImage(filePath, fileName, file, 1500);
                    GraphicsHelper.SaveOriginalImageWithDefinedDimentions(filePath, fileName, f, 400, 283, ScaleMode.Crop);

                    var ai = new ArticleImage
                    {
                        ImageSrc = fileName
                    };

                    article.ArticleImages.Add(ai);
                }

                _context.SaveChanges();

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