Exemplo n.º 1
0
        public ActionResult Create(Picture picture)
        {
            if (ModelState.IsValid)
            {
                var file = Request.Files[0];
                if (file != null && file.ContentLength > 0)
                {
                    var path = Server.MapPath("~/Images/album/" + picture.AlbumID + "/");
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }

                    var extension = Path.GetExtension(file.FileName);
                    var fileName = Guid.NewGuid().ToString() + extension;
                    file.SaveAs(Path.Combine(path, fileName));
                    picture.Path = fileName;
                }

                db.Pictures.Add(picture);
                db.SaveChanges();
                return RedirectToAction("Details", "Album", new { id = picture.AlbumID });
            }

            return View(picture);
        }
Exemplo n.º 2
0
        public ActionResult Edit(Picture picture)
        {
            if (ModelState.IsValid)
            {
                var old = db.Pictures.Single(a => a.PictureID == picture.PictureID);
                picture.Path = old.Path;

                var file = Request.Files[0];
                if (file != null && file.ContentLength > 0)
                {
                    var path = Server.MapPath("~/Images/album/"+picture.AlbumID+"/");
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }

                    var extension = Path.GetExtension(file.FileName);
                    var fileName = Guid.NewGuid().ToString() + extension;
                    file.SaveAs(Path.Combine(path, fileName));
                    picture.Path = fileName;

                    // delete old Path
                    if (!string.IsNullOrEmpty(old.Path))
                    {
                        System.IO.File.Delete(Path.Combine(path, old.Path));
                    }
                }

                db.Entry(old).CurrentValues.SetValues(picture);
                db.SaveChanges();
                return RedirectToAction("Details", "Album", new { id = picture.AlbumID });
            }
            return View(picture);
        }
Exemplo n.º 3
0
        //
        // GET: /Picture/Create

        public ActionResult Create(int id)
        {
            Picture picture = new Picture();
            picture.AlbumID = id;
            return View(picture);
        }