Exemplo n.º 1
0
        public ActionResult Edit([Bind(Include = "ElementoID,PoiID,nome,descricao")] Elemento elemento, HttpPostedFileBase upload)
        {
            var elementoToUpdate = db.Elemento.Find(elemento.ElementoID);

            try
            {
                if (TryUpdateModel(elementoToUpdate, "", new string[] { "ElementoID", "PoiID", "nome", "descricao" }))
                {
                    try
                    {
                        if (upload != null && upload.ContentLength > 0)
                        {
                            string[] allowedImageExtensions = { ".gif", ".png", ".jpeg", ".jpg" };

                            String fileExtension = Path.GetExtension(upload.FileName);
                            foreach (string t in allowedImageExtensions)
                            {
                                if (fileExtension == t)
                                {
                                    string   currentFilePath = elementoToUpdate.ImagemElemento;
                                    FileInfo file            = new FileInfo(Path.Combine(Server.MapPath("~/Content/Images/GaleriaElemento/Imagem"), currentFilePath));
                                    file.Delete();

                                    var FileName  = Guid.NewGuid().ToString() + Path.GetExtension(upload.FileName);
                                    var FileTypes = FileType.Imagem;
                                    elementoToUpdate.ImagemElemento = FileName;
                                    elementoToUpdate.FileType       = FileTypes;

                                    upload.SaveAs(Path.Combine(Server.MapPath("~/Content/Images/GaleriaElemento/Imagem"), FileName));
                                }
                            }
                        }
                    }
                    catch (RetryLimitExceededException)
                    {
                        ModelState.AddModelError("", "Erro ao guardar alterações, tente mais tarde. Se o problema persistir contate o administrador do sistema.");
                    }
                }
            }
            catch (Exception)
            {
            }

            if (ModelState.IsValid)
            {
                db.Entry(elementoToUpdate).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            ViewBag.PoiID = new SelectList(db.Poi, "PoiID", "nome", elemento.PoiID);
            return(View(elemento));
        }
Exemplo n.º 2
0
        public ActionResult EditPost(int?id, HttpPostedFileBase upload)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            var poiToUpdate = db.Poi.Find(id);

            if (TryUpdateModel(poiToUpdate, "", new string[] { "nome", "latitude", "longitude", "descricao", "resumo", "categoriaID" }))
            {
                try
                {
                    if (upload != null && upload.ContentLength > 0)
                    {
                        string[] allowedImageExtensions = { ".gif", ".png", ".jpeg", ".jpg" };

                        String fileExtension = Path.GetExtension(upload.FileName);
                        foreach (string t in allowedImageExtensions)
                        {
                            if (fileExtension == t)
                            {
                                string   currentFilePath = poiToUpdate.ImagemPath;
                                FileInfo file            = new FileInfo(Path.Combine(Server.MapPath("~/Content/Images/GaleriaPoi/Imagem"), currentFilePath));
                                file.Delete();

                                var FileName  = Guid.NewGuid().ToString() + Path.GetExtension(upload.FileName);
                                var FileTypes = FileType.Imagem;
                                poiToUpdate.ImagemPath = FileName;
                                poiToUpdate.FileType   = FileTypes;

                                upload.SaveAs(Path.Combine(Server.MapPath("~/Content/Images/GaleriaPoi/Imagem"), FileName));
                            }
                        }
                    }
                    if (ModelState.IsValid)
                    {
                        db.Entry(poiToUpdate).State = EntityState.Modified;
                        db.SaveChanges();
                        return(RedirectToAction("Index"));
                    }
                }
                catch (RetryLimitExceededException)
                {
                    ModelState.AddModelError("", "Erro ao guardar as alterações. Tente novamente, se persistir contacte o administrador.");
                }
            }
            PopulateCategoriasDropDownList(poiToUpdate.CategoriaID);
            return(View(poiToUpdate));
        }
Exemplo n.º 3
0
        public ActionResult Edit([Bind(Include = "GaleriaElementoID,ElementoID,legenda")] GaleriaElemento galeriaElemento, HttpPostedFileBase upload)
        {
            var galeriaElementoToUpdate = db.GaleriaElemento.Find(galeriaElemento.GaleriaElementoID);

            if (ModelState.IsValid)
            {
                if (TryUpdateModel(galeriaElementoToUpdate, "", new string[] { "GaleriaElementoID,ElementoID,legenda" }))
                {
                    try
                    {
                        if (upload != null && upload.ContentLength > 0)
                        {
                            //Apaga a lista dos ficheiros associados e o ficheiro local associado
                            if (galeriaElementoToUpdate.FilePathElementos.Any(f => f.FileType == FileType.Imagem))
                            {
                                string currentFilePath = galeriaElementoToUpdate.FilePathElementos.First().FileName;
                                db.FilePathsEl.Remove(
                                    galeriaElementoToUpdate.FilePathElementos.First(f => f.FileType == FileType.Imagem));
                                FileInfo file =
                                    new FileInfo(Path.Combine(Server.MapPath("~/Content/Images/GaleriaElemento/"), currentFilePath));
                                file.Delete();
                            }
                            else if (galeriaElementoToUpdate.FilePathElementos.Any(f => f.FileType == FileType.Audio))
                            {
                                string currentFilePath = galeriaElementoToUpdate.FilePathElementos.First().FileName;
                                db.FilePathsEl.Remove(
                                    galeriaElementoToUpdate.FilePathElementos.First(f => f.FileType == FileType.Audio));
                                FileInfo file =
                                    new FileInfo(Path.Combine(Server.MapPath("~/Content/Images/GaleriaElemento/"), currentFilePath));
                                file.Delete();
                            }
                            else if (galeriaElementoToUpdate.FilePathElementos.Any(f => f.FileType == FileType.Video))
                            {
                                string currentFilePath = galeriaElementoToUpdate.FilePathElementos.First().FileName;
                                db.FilePathsEl.Remove(galeriaElementoToUpdate.FilePathElementos.First(f => f.FileType == FileType.Video));
                                FileInfo file =
                                    new FileInfo(Path.Combine(Server.MapPath("~/Content/Videos/GaleriaElemento/"), currentFilePath));
                                file.Delete();
                            }
                            //Verifica se o novo ficheiro é de uma extenção válida e adiciona-o à lista de ficheiros e insere localmente na pasta correspondente
                            string[] allowedImageExtensions = { ".png", ".jpeg", ".jpg" };
                            string[] allowedVideoExtensions = { ".gif" };
                            string[] allowedAudioExtensions = { ".mp4", ".mp3", ".wav" };
                            String   fileExtension          = Path.GetExtension(upload.FileName);
                            for (int i = 0; i < allowedImageExtensions.Length; i++)
                            {
                                if (fileExtension == allowedImageExtensions[i])
                                {
                                    var file = new FilePathElemento
                                    {
                                        FileName = Guid.NewGuid().ToString() + Path.GetExtension(upload.FileName),
                                        FileType = FileType.Imagem
                                    };
                                    galeriaElementoToUpdate.FilePathElementos.Add(file);
                                    upload.SaveAs(Path.Combine(Server.MapPath("~/Content/Images/GaleriaElemento/"), file.FileName));
                                }
                            }
                            for (int i = 0; i < allowedVideoExtensions.Length; i++)
                            {
                                if (fileExtension == allowedVideoExtensions[i])
                                {
                                    var file = new FilePathElemento
                                    {
                                        FileName = Guid.NewGuid().ToString() + Path.GetExtension(upload.FileName),
                                        FileType = FileType.Video
                                    };

                                    galeriaElementoToUpdate.FilePathElementos.Add(file);
                                    upload.SaveAs(Path.Combine(Server.MapPath("~/Content/Videos/GaleriaElemento"), file.FileName));
                                }
                            }
                            for (int i = 0; i < allowedAudioExtensions.Length; i++)
                            {
                                if (fileExtension == allowedAudioExtensions[i])
                                {
                                    var file = new FilePathElemento()
                                    {
                                        FileName = Guid.NewGuid().ToString() + Path.GetExtension(upload.FileName),
                                        FileType = FileType.Audio
                                    };

                                    galeriaElementoToUpdate.FilePathElementos.Add(file);
                                    upload.SaveAs(Path.Combine(Server.MapPath("~/Content/Audio/GaleriaElemento/"), file.FileName));
                                }
                            }
                        }
                        db.Entry(galeriaElementoToUpdate).State = EntityState.Modified;
                        db.SaveChanges();
                        return(RedirectToAction("Index"));
                    }
                    catch (RetryLimitExceededException /* dex */)
                    {
                        //Log the error (uncomment dex variable name and add a line here to write a log.
                        ModelState.AddModelError("",
                                                 "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
                    }
                }
            }
            ViewBag.ElementoID = new SelectList(db.Elemento, "ElementoID", "nome", galeriaElemento.ElementoID);
            return(View(galeriaElemento));
        }
Exemplo n.º 4
0
        public ActionResult Edit([Bind(Include = "CategoriaID,nome,genero")] Categoria categoria, HttpPostedFileBase upload)
        {
            Categoria categoriaToUpdate = db.Categoria.Find(categoria.CategoriaID);
            bool      validName         = false;

            try
            {
                foreach (var c in db.Categoria)
                {
                    if (upload.FileName.Equals(c.nome))
                    {
                        validName = true;
                    }
                }
                string nomeF = categoria.nome + Path.GetExtension(upload.FileName);
                if (nomeF.Equals(upload.FileName))
                {
                    validName = true;
                }
            }
            catch (Exception)
            {
                ModelState.AddModelError("", "Tem que introduzir um ficheiro");
            }
            if (!validName)
            {
                return(View(categoria));
            }
            if (ModelState.IsValid)
            {
                if (TryUpdateModel(categoriaToUpdate, "",
                                   new string[] { "CategoriaID", "nome", "genero", "FilePathCategoria" }))
                {
                    try
                    {
                        if (upload != null && upload.ContentLength > 0)
                        {
                            if (categoriaToUpdate.FilePathCategoria != null)
                            {
                                string currentFilePath = categoriaToUpdate.FilePathCategoria.FileName;

                                db.FilePathCats.Remove(categoriaToUpdate.FilePathCategoria);
                                FileInfo file =
                                    new FileInfo(Path.Combine(Server.MapPath("~/Content/Images/Icones"), currentFilePath));
                                file.Delete();
                            }
                        }
                        string[] allowedImageExtensions = { ".gif", ".png", ".jpeg", ".jpg" };

                        String fileExtension = Path.GetExtension(upload.FileName);
                        if (upload != null && upload.ContentLength > 0)
                        {
                            for (int i = 0; i < allowedImageExtensions.Length; i++)
                            {
                                if (fileExtension == allowedImageExtensions[i])
                                {
                                    var FileName  = upload.FileName;
                                    var FileTypes = FileType.Imagem;

                                    categoria.FilePathCategoria          = new FilePathCategoria();
                                    categoria.FilePathCategoria.FileName = FileName;
                                    categoria.FilePathCategoria.FileType = FileTypes;
                                    upload.SaveAs(Path.Combine(Server.MapPath("~/Content/Images/Icones"), FileName));
                                }
                            }
                        }
                        db.Entry(categoriaToUpdate).State = EntityState.Modified;
                        db.SaveChanges();
                        return(RedirectToAction("Index"));
                    }
                    catch (RetryLimitExceededException)
                    {
                        ModelState.AddModelError("",
                                                 "Erro a guardar as alterações. Tente outra vez, se o problema persitir contate o administrador do sistema");
                    }
                }
            }
            return(View(categoriaToUpdate));
        }