public FileResult Download(int documentId) { DocumentNonOfficiel document = _documentNonOfficielService.Get(documentId); if (document == null) { throw new HttpException(404, "Document n'existe pas"); } return(File(Server.MapPath(document.Emplacement), System.Net.Mime.MediaTypeNames.Application.Octet, document.Titre + "." + document.Type)); }
public ActionResult AddDocument(HttpPostedFileBase document, int module) { if (document != null && document.ContentLength > 0) { var extension = Path.GetExtension(document.FileName); var type = ""; if (extension.ToLower().Equals(".pdf") || extension.ToLower().Equals(".doc") || extension.ToLower().Equals(".docx") || extension.ToLower().Equals(".ppt") || extension.ToLower().Equals(".pptx") || extension.ToLower().Equals(".xls") || extension.ToLower().Equals(".xlsx") || extension.ToLower().Equals(".xml") ) { type = extension.Substring(1).ToLower(); } else { return(View("Add")); } var titre = Path.GetFileNameWithoutExtension(document.FileName); var path = "~/Resources/Documents/" + Path.GetFileName(document.FileName); if (Session["user"] is Etudiant) { DocumentNonOfficiel documentNonOfficiel = new DocumentNonOfficiel { Titre = titre, Emplacement = path, Type = type, DateAjoutNonOfficiel = DateTime.Now, ModuleId = module, EtudiantId = ((Etudiant)Session["user"]).EtudiantId }; documentNonOfficiel = _documentNonOfficielService.Add(documentNonOfficiel); string targetPath = Server.MapPath("~/Resources/Documents"); if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } if (documentNonOfficiel != null) { document.SaveAs(Path.Combine(targetPath, Path.GetFileName(document.FileName))); } return(RedirectToAction("NonOfficiel", "Document")); } else if (Session["user"] is Professeur) { DocumentOfficiel documentOfficiel = new DocumentOfficiel { Titre = titre, Emplacement = path, Type = type, DateAjoutOfficiel = DateTime.Now, ModuleId = module, ProfesseurId = ((Professeur)Session["user"]).ProfesseurId }; documentOfficiel = _documentOfficielService.Add(documentOfficiel); if (documentOfficiel != null) { document.SaveAs(Path.Combine(Server.MapPath("~/Resources/Documents"), Path.GetFileName(document.FileName))); } return(RedirectToAction("Officiel", "Document")); } } return(new HttpNotFoundResult()); }