Beispiel #1
0
        public ActionResult Edit(Board rec)
        {
            if (ModelState.IsValid)
            {
                HttpPostedFileBase file = Request.Files["imagefile"];
                DbEntities tempdb = new DbEntities();
                Board ori = tempdb.Board.Find(rec.Id);
                if (file != null && file.FileName != "")
                {
                    if (ori.ImageUrl != null)
                    {
                        string oriFilePath = Path.Combine(HttpContext.Server.MapPath("/Uploads"), Path.GetFileName(ori.ImageUrl));
                        if (System.IO.File.Exists(oriFilePath))
                        {
                            System.IO.File.Delete(oriFilePath);
                        }
                    }

                    string ext = Path.GetExtension(file.FileName);
                    string filename = Guid.NewGuid().ToString() + ext;
                    string filePath = Path.Combine(HttpContext.Server.MapPath("/Uploads"), filename);
                    file.SaveAs(filePath);
                    rec.ImageUrl = "/Uploads/" + filename;
                }
                else
                {
                    rec.ImageUrl = ori.ImageUrl;
                }

                db.Entry(rec).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Board", "Admin");
            }
            return View(rec);
        }
Beispiel #2
0
        public ActionResult Create(Board rec)
        {
            if (ModelState.IsValid)
            {
                HttpPostedFileBase file = Request.Files["imagefile"];
                if (file != null && file.FileName != "")
                {
                    string ext = Path.GetExtension(file.FileName);
                    string filename = Guid.NewGuid().ToString() + ext;
                    string filePath = Path.Combine(HttpContext.Server.MapPath("/Uploads"), filename);
                    file.SaveAs(filePath);
                    rec.ImageUrl = "/Uploads/" + filename;
                }
                db.Board.Add(rec);
                db.SaveChanges();
                return RedirectToAction("Board", "Admin");
            }

            return View(rec);
        }