Example #1
0
        public ActionResult Create([Bind(Include = "ID,DescriptionOfNotice,NoticeDownloadUrl")] GeneralNoticeModels generalNoticeModels, HttpPostedFileBase file1)
        {
            string subPath = "~/GeneralNotice"; // your code goes here
            bool   exists  = System.IO.Directory.Exists(Server.MapPath(subPath));

            if (!exists)
            {
                System.IO.Directory.CreateDirectory(Server.MapPath(subPath));
            }
            subPath = "~/GeneralNotice";

            if (file1 == null)
            {
                ModelState.AddModelError("", "File is not attached!");
            }

            HttpPostedFileBase file = file1;

            if (ModelState.IsValid)
            {
                ////////////////
                if (file != null)
                {
                    var allowedExtensions = new[] { ".pdf" };
                    var fileName          = Path.GetFileName(file.FileName);
                    var ext = Path.GetExtension(file.FileName);                     //getting the extension(ex-.jpg)
                    if (allowedExtensions.Contains(ext))                            //check what type of extension
                    {
                        string name   = Path.GetFileNameWithoutExtension(fileName); //getting file name without extension
                        string myfile = name + ext;                                 //appending the name with id
                                                                                    // store the file inside ~/project folder(Img)
                        var path = Path.Combine(Server.MapPath("~/GeneralNotice"), myfile);

                        if (System.IO.File.Exists(path))
                        {
                            System.IO.File.Delete(path);
                        }

                        generalNoticeModels.NoticeDownloadUrl = Path.Combine("~/GeneralNotice", myfile);

                        file.SaveAs(path);
                    }
                }
                ///////////////
                db.GeneralNoticeModels.Add(generalNoticeModels);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(generalNoticeModels));
        }
Example #2
0
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            GeneralNoticeModels generalNoticeModels = db.GeneralNoticeModels.Find(id);

            if (generalNoticeModels == null)
            {
                return(HttpNotFound());
            }
            return(View(generalNoticeModels));
        }
Example #3
0
        public ActionResult DeleteConfirmed(int id)
        {
            var path = Path.Combine(Server.MapPath(db.GeneralNoticeModels.Where(m => m.ID == id).Select(m => m.NoticeDownloadUrl).FirstOrDefault()));

            if (System.IO.File.Exists(path))
            {
                System.IO.File.Delete(path);
            }

            GeneralNoticeModels generalNoticeModels = db.GeneralNoticeModels.Find(id);

            db.GeneralNoticeModels.Remove(generalNoticeModels);
            db.SaveChanges();



            return(RedirectToAction("Index"));
        }
Example #4
0
        public ActionResult Edit([Bind(Include = "ID,DescriptionOfNotice,NoticeDownloadUrl")] GeneralNoticeModels generalNoticeModels, HttpPostedFileBase file1)
        {
            HttpPostedFileBase file = file1;

            var Prevpath = Path.Combine(Server.MapPath(db.GeneralNoticeModels.Where(m => m.ID == generalNoticeModels.ID).Select(m => m.NoticeDownloadUrl).FirstOrDefault()));

            generalNoticeModels.NoticeDownloadUrl = db.GeneralNoticeModels.Where(m => m.ID == generalNoticeModels.ID).Select(m => m.NoticeDownloadUrl).FirstOrDefault();
            if (System.IO.File.Exists(Prevpath) && file != null)
            {
                System.IO.File.Delete(Prevpath);
            }

            if (ModelState.IsValid)
            {
                ////////////////
                if (file != null)
                {
                    var allowedExtensions = new[] { ".pdf" };
                    var fileName          = Path.GetFileName(file.FileName);
                    var ext = Path.GetExtension(file.FileName);                     //getting the extension(ex-.jpg)
                    if (allowedExtensions.Contains(ext))                            //check what type of extension
                    {
                        string name   = Path.GetFileNameWithoutExtension(fileName); //getting file name without extension
                        string myfile = name + ext;                                 //appending the name with id
                                                                                    // store the file inside ~/project folder(Img)
                        var path = Path.Combine(Server.MapPath("~/GeneralNotice"), myfile);

                        if (System.IO.File.Exists(path))
                        {
                            System.IO.File.Delete(path);
                        }

                        generalNoticeModels.NoticeDownloadUrl = Path.Combine("~/GeneralNotice", myfile);

                        file.SaveAs(path);
                    }
                }
                ///////////////
                db.Entry(generalNoticeModels).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(View(generalNoticeModels));
        }