Ejemplo n.º 1
0
        public ActionResult Create(ArticleDto article, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                // Insert article in database
                using (var database = new BlogEntities())
                {
                    var authorId = database.AspNetUsers.Where(u => u.UserName == this.User.Identity.Name).First().Id;

                    // Set articlea author
                    article.AuthorId = authorId;

                    article.DatePost = DateTime.Now;

                    // Upload image. Check allowed types.
                    if (image != null)
                    {
                        var allowedContentTypes = new[] { "image/jpeg", "image/jpg", "image/png", "image/gif", "image/tif" };

                        if (allowedContentTypes.Contains(image.ContentType))
                        {
                            var imagesPath   = "/Content/Images/";
                            var filename     = Guid.NewGuid().ToString() + image.FileName;
                            var uploadPath   = imagesPath + filename;
                            var physicalPath = Server.MapPath(uploadPath);
                            image.SaveAs(physicalPath);
                            article.ImagePath = uploadPath;
                        }
                    }

                    OpArticleInsert op = new OpArticleInsert();
                    op.Article = article;
                    OperationResult result = OperationManager.Singleton.ExecuteOperation(op);

                    if (result.Status)
                    {
                        TempData["Success"] = "Added Successfully!";
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        return(RedirectToAction("Create"));
                    }
                }
            }

            return(View(article));
        }
Ejemplo n.º 2
0
        public ActionResult Create(ArticleDto article, HttpPostedFileBase image)
        {
            // Author is admin
            article.AuthorId = "cb646481-f6b9-4874-a7f6-b90f329cabb1";

            article.DatePost = DateTime.Now;

            // Upload image. Check allowed types.
            if (image != null)
            {
                var allowedContentTypes = new[] { "image/jpeg", "image/jpg", "image/png", "image/gif", "image/tif" };

                if (allowedContentTypes.Contains(image.ContentType))
                {
                    var imagesPath   = "/Content/Images/";
                    var filename     = Guid.NewGuid().ToString() + image.FileName;
                    var uploadPath   = imagesPath + filename;
                    var physicalPath = Server.MapPath(uploadPath);
                    image.SaveAs(physicalPath);
                    article.ImagePath = uploadPath;
                }
            }
            else
            {
                return(RedirectToAction("Create"));
            }


            OpArticleInsert op = new OpArticleInsert();

            op.Article = article;
            OperationResult result = OperationManager.Singleton.ExecuteOperation(op);


            if (result.Status)
            {
                TempData["Success"] = "Added Successfully!";
                return(RedirectToAction("Index"));
            }
            else
            {
                return(RedirectToAction("Create"));
            }
        }