Example #1
0
        public ActionResult NewArticle(string Name, string Topic, string Text, HttpPostedFileBase Picture)
        {
            try
            {
                PezeshkPlusEntities db = new PezeshkPlusEntities();
                string picAddress;
                if (Picture != null)
                {
                    if (Path.GetExtension(Picture.FileName) != ".jpg" && Path.GetExtension(Picture.FileName) != ".JPG" && Path.GetExtension(Picture.FileName) != ".png" && Path.GetExtension(Picture.FileName) != ".PNG" && Path.GetExtension(Picture.FileName) != ".jpeg")
                    {
                        TempData["PictureError"] = "پسوند فایل ارسالی باید jpg یا jpeg یا png باشد";
                        return(RedirectToAction("Panel"));
                    }
                    if (Picture.ContentLength > 1 & Picture.ContentLength < (5 * 1024 * 1024))
                    {
                        string extention   = Path.GetExtension(Picture.FileName);
                        string guidNamePic = Guid.NewGuid().ToString();
                        picAddress = $"/Images/Article/{guidNamePic}{extention}";
                        Picture.SaveAs(Server.MapPath("~" + picAddress));
                    }
                    else
                    {
                        TempData["PictureError"] = "سایز فایل عکس ارسال شده باید حداکثر 5 Mg باشد";
                        return(RedirectToAction("Panel"));
                    }
                }
                else
                {
                    TempData["PictureError"] = "عکس اجباری است";
                    return(RedirectToAction("Panel"));
                }

                db.USP_INS_Article(Topic, Text, Name, picAddress);
                TempData["ArticleSent"] = "مقاله ثبت شد";
            }
            catch (Exception)
            {
                TempData["ArticleNotSent"] = "خطا مقاله ثبت نشد";
            }
            return(RedirectToAction("Panel"));
        }