Пример #1
0
        public ActionResult Edit(FilmViewModel filmViewModel, HttpPostedFileBase upload)
        {
            if (ModelState.IsValid)
            {
                filmViewModel.ThisFilm.ImageId = filmViewModel.ThisFilmImage.ImageId;

                // Gets the dropdownlist selection and adds the id to the viewmodel.
                filmViewModel.ThisFilm.GenreId       = Int32.Parse(Request["Genres"]);
                filmViewModel.ThisFilm.CertificateId = Int32.Parse(Request["Certificates"]);


                if (upload != null && upload.ContentLength > 0)
                {
                    if (upload.ContentType == "image/jpeg" ||
                        upload.ContentType == "image/jpg" ||
                        upload.ContentType == "image/gif" ||
                        upload.ContentType == "image/png")
                    {
                        // If an image is being uploaded then save the image to a
                        // temporary folder, convert the image to bytes, save the image
                        // as a new record and assign its new id to the film model for
                        // updating the db.
                        if (Request.Files.Count > 0)
                        {
                            var file = Request.Files[0];
                            {
                                // Save image to temp folder.
                                var fileName = Path.GetFileName(file.FileName);
                                var path     = Path.Combine(Server.MapPath("~/ImagesTemp/"), fileName);
                                file.SaveAs(path);

                                // Convert image to bytes.
                                Image     newImage  = Image.FromFile(path);
                                FilmImage filmImage = new FilmImage();
                                filmImage.ImageBytes = filmImage.ConvertImageToByteArray(newImage);

                                // Insert image into db and return the new image id.
                                db.FilmImages.Add(filmImage);
                                db.SaveChanges();
                                int imageId = filmImage.ImageId;

                                //Get the old film image and delete it.
                                FilmImage oldFilmImage = db.FilmImages.Where(x => x.ImageId == filmViewModel.ThisFilmImage.ImageId).Single();
                                db.FilmImages.Remove(oldFilmImage);

                                filmViewModel.ThisFilm.ImageId = imageId;

                                // Attempt to delete temporary image.
                                if (System.IO.File.Exists(path))
                                {
                                    try
                                    {
                                        System.IO.File.Delete(path);
                                    }
                                    catch (Exception)
                                    {
                                        // Do nothing, temporary folder will be cleared
                                        // when application is re-launched.
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        // Constructs error messages for the view.
                        ViewBag.Message = "Not valid image format";
                    }
                }


                //Update the db and save.
                db.Entry(filmViewModel.ThisFilm).State = EntityState.Modified;
                db.SaveChanges();

                return(RedirectToAction("Index"));
            }
            return(View(filmViewModel));
        }
Пример #2
0
        public ActionResult Create(
            [Bind(Include = "FilmID,GenreID,CertificateID,ImageID," +
                            "Title,Synopsis,Runtime,ReleaseDate")] Film film,
            HttpPostedFileBase upload)
        {
            if (ModelState.IsValid)
            {
                if (upload != null && upload.ContentLength > 0)
                {
                    if (upload.ContentType == "image/jpeg" ||
                        upload.ContentType == "image/jpg" ||
                        upload.ContentType == "image/gif" ||
                        upload.ContentType == "image/png")
                    {
                        // If an image is being uploaded then save the image to a temporary folder,
                        // convert the image to bytes and assign it to the model for inserting into the db.
                        if (Request.Files.Count > 0)
                        {
                            var file = Request.Files[0];

                            // Save image to temp folder.
                            var fileName = Path.GetFileName(file.FileName);
                            var path     = Path.Combine(Server.MapPath("~/ImagesTemp/"), fileName);
                            file.SaveAs(path);

                            // Convert image to bytes.
                            Image     newImage  = Image.FromFile(path);
                            FilmImage filmImage = new FilmImage();
                            filmImage.ImageBytes = filmImage.ConvertImageToByteArray(newImage);

                            // Insert image into db and return the new image id.
                            db.FilmImages.Add(filmImage);
                            db.SaveChanges();
                            int imageId = filmImage.ImageId;
                            film.ImageId = imageId;

                            // Attempt to delete temporary image.
                            if (System.IO.File.Exists(path))
                            {
                                try
                                {
                                    System.IO.File.Delete(path);
                                }
                                catch (Exception thisException)
                                {
                                    // Do nothing, temporary folder will be
                                    // cleared when application is re-launched.
                                }
                            }
                            // Add the film to the database and save.
                            film.GenreId       = Int32.Parse(Request["Genres"]);
                            film.CertificateId = Int32.Parse(Request["Certificates"]);

                            db.Films.Add(film);
                            db.SaveChanges();
                        }
                    }
                    else
                    {
                        // Constructs error messages for the view.
                        ViewBag.ErrorMessage = "A valid image image format was not uploaded.";
                    }
                }
                else
                {
                    // Constructs error messages for the view.
                    ViewBag.ErrorMessage = "An image must be uploaded.";
                }

                return(RedirectToAction("Index", "Films", new { errorMessage = ViewBag.ErrorMessage }));
            }

            return(View(film));
        }