Example #1
0
        public ActionResult Create(Publication rec)
        {
            if (ModelState.IsValid)
            {
                HttpPostedFileBase file = Request.Files["file"];
                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.FileUrl = "/Uploads/" + filename;
                }

                HttpPostedFileBase imagefile = Request.Files["imagefile"];
                if (imagefile != null && imagefile.FileName != "")
                {
                    string ext = Path.GetExtension(imagefile.FileName);
                    string filename = Guid.NewGuid().ToString() + ext;
                    string filePath = Path.Combine(HttpContext.Server.MapPath("/Uploads"), filename);
                    imagefile.SaveAs(filePath);
                    rec.ImageUrl = "/Uploads/" + filename;
                }
                db.Publication.Add(rec);
                db.SaveChanges();
                return RedirectToAction("Publication", "Admin");
            }

            return View(rec);
        }
Example #2
0
        public ActionResult Edit(Publication rec)
        {
            if (ModelState.IsValid)
            {
                HttpPostedFileBase file = Request.Files["file"];
                DbEntities tempdb = new DbEntities();
                Publication ori = tempdb.Publication.Find(rec.Id);
                if (file != null && file.FileName != "")
                {
                    if (ori.FileUrl != null)
                    {
                        string oriFilePath = Path.Combine(HttpContext.Server.MapPath("/Uploads"), Path.GetFileName(ori.FileUrl));
                        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.FileUrl = "/Uploads/" + filename;
                }
                else
                {
                    rec.FileUrl = ori.FileUrl;
                }

                HttpPostedFileBase imagefile = Request.Files["imagefile"];
                if (imagefile != null && imagefile.FileName != "")
                {
                    if (ori.ImageUrl != null)
                    {
                        string oriImageFilePath = Path.Combine(HttpContext.Server.MapPath("/Uploads"), Path.GetFileName(ori.ImageUrl));
                        if (System.IO.File.Exists(oriImageFilePath))
                        {
                            System.IO.File.Delete(oriImageFilePath);
                        }
                    }
                    string ext = Path.GetExtension(imagefile.FileName);
                    string filename = Guid.NewGuid().ToString() + ext;
                    string filePath = Path.Combine(HttpContext.Server.MapPath("/Uploads"), filename);
                    imagefile.SaveAs(filePath);
                    rec.ImageUrl = "/Uploads/" + filename;
                }
                else
                {
                    rec.ImageUrl = ori.ImageUrl;
                }

                db.Entry(rec).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Publication", "Admin");
            }
            return View(rec);
        }