public ActionResult Create([Bind(Include = "GaleriaPoiID,PoiID,Legenda")] GaleriaPoi galeriaPoi, HttpPostedFileBase upload) { try { String fileExtension = Path.GetExtension(upload.FileName); if (ModelState.IsValid) { string[] allowedImageExtensions = { ".gif", ".png", ".jpeg", ".jpg" }; string[] allowedVideoExtensions = { ".mp4" }; string[] allowedAudioExtensions = { ".mp4", ".mp3", ".wav" }; if (upload != null && upload.ContentLength > 0) { for (int i = 0; i < allowedImageExtensions.Length; i++) { if (fileExtension == allowedImageExtensions[i]) { var file = new FilePathPoi { FileName = Guid.NewGuid().ToString() + Path.GetExtension(upload.FileName), FileType = FileType.Imagem }; galeriaPoi.FilePaths = new List <FilePathPoi>(); galeriaPoi.FilePaths.Add(file); upload.SaveAs(Path.Combine(Server.MapPath("~/Content/Images/GaleriaPoi/"), file.FileName)); } } for (int i = 0; i < allowedVideoExtensions.Length; i++) { if (fileExtension == allowedVideoExtensions[i]) { var file = new FilePathPoi { FileName = Guid.NewGuid().ToString() + Path.GetExtension(upload.FileName), FileType = FileType.Video }; galeriaPoi.FilePaths = new List <FilePathPoi>(); galeriaPoi.FilePaths.Add(file); upload.SaveAs(Path.Combine(Server.MapPath("~/Content/Videos/Poi/"), file.FileName)); } } for (int i = 0; i < allowedAudioExtensions.Length; i++) { if (fileExtension == allowedAudioExtensions[i]) { var file = new FilePathPoi() { FileName = Guid.NewGuid().ToString() + Path.GetExtension(upload.FileName), FileType = FileType.Audio }; galeriaPoi.FilePaths = new List <FilePathPoi>(); galeriaPoi.FilePaths.Add(file); upload.SaveAs(Path.Combine(Server.MapPath("~/Content/Audio/GaleriaPoi/"), file.FileName)); } } db.GaleriaPoi.Add(galeriaPoi); db.SaveChanges(); return(RedirectToAction("Index")); } } } catch (Exception) { ModelState.AddModelError("", "Tem que introduzir um ficheiro"); } ViewBag.PoiID = new SelectList(db.Poi, "PoiID", "nome", galeriaPoi.PoiID); return(View(galeriaPoi)); }
public ActionResult Edit([Bind(Include = "GaleriaPoiID,PoiID,Legenda")] GaleriaPoi galeriaPoi, HttpPostedFileBase upload) { var galeriaPoiToUpdate = db.GaleriaPoi.Find(galeriaPoi.GaleriaPoiID); if (ModelState.IsValid) { if (TryUpdateModel(galeriaPoiToUpdate, "", new string[] { "GaleriaPoiID", "PoiID", "Legenda" })) { try { if (upload != null && upload.ContentLength > 0) { //Apaga a lista dos ficheiros associados e o ficheiro local associado if (galeriaPoiToUpdate.FilePaths.Any(f => f.FileType == FileType.Imagem)) { string currentFilePath = galeriaPoiToUpdate.FilePaths.First().FileName; db.FilePaths.Remove(galeriaPoiToUpdate.FilePaths.First(f => f.FileType == FileType.Imagem)); FileInfo file = new FileInfo(Path.Combine(Server.MapPath("~/Content/Images/GaleriaPoi/"), currentFilePath)); file.Delete(); } else if (galeriaPoiToUpdate.FilePaths.Any(f => f.FileType == FileType.Audio)) { string currentFilePath = galeriaPoiToUpdate.FilePaths.First().FileName; db.FilePaths.Remove(galeriaPoiToUpdate.FilePaths.First(f => f.FileType == FileType.Audio)); FileInfo file = new FileInfo(Path.Combine(Server.MapPath("~/Content/Audio/Poi/"), currentFilePath)); file.Delete(); } else if (galeriaPoiToUpdate.FilePaths.Any(f => f.FileType == FileType.Video)) { string currentFilePath = galeriaPoiToUpdate.FilePaths.First().FileName; db.FilePaths.Remove(galeriaPoiToUpdate.FilePaths.First(f => f.FileType == FileType.Video)); FileInfo file = new FileInfo(Path.Combine(Server.MapPath("~/Content/Videos/Poi/"), 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 FilePathPoi { FileName = Guid.NewGuid().ToString() + Path.GetExtension(upload.FileName), FileType = FileType.Imagem }; galeriaPoiToUpdate.FilePaths.Add(file); upload.SaveAs(Path.Combine(Server.MapPath("~/Content/Images/GaleriaPoi/"), file.FileName)); } } for (int i = 0; i < allowedVideoExtensions.Length; i++) { if (fileExtension == allowedVideoExtensions[i]) { var file = new FilePathPoi { FileName = Guid.NewGuid().ToString() + Path.GetExtension(upload.FileName), FileType = FileType.Video }; galeriaPoiToUpdate.FilePaths = new List <FilePathPoi>(); galeriaPoiToUpdate.FilePaths.Add(file); upload.SaveAs(Path.Combine(Server.MapPath("~/Content/Videos/Poi"), file.FileName)); } } for (int i = 0; i < allowedAudioExtensions.Length; i++) { if (fileExtension == allowedAudioExtensions[i]) { var file = new FilePathPoi() { FileName = Guid.NewGuid().ToString() + Path.GetExtension(upload.FileName), FileType = FileType.Audio }; galeriaPoi.FilePaths = new List <FilePathPoi>(); galeriaPoi.FilePaths.Add(file); upload.SaveAs(Path.Combine(Server.MapPath("~/Content/Audio/GaleriaPoi/"), file.FileName)); } } } db.Entry(galeriaPoiToUpdate).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("", "Erro ao guardar alterações, tente mais tarde. Se o problema persistir contate o administrador do sistema."); } } } ViewBag.PoiID = new SelectList(db.Poi, "PoiID", "nome", galeriaPoiToUpdate.PoiID); return(View(galeriaPoiToUpdate)); }