/** * Cria um novo template com o ficheiro respetivo */ public IActionResult CreateTemplate(IFormFile fil, DateTime data, string comments, int userId, int activityId, String documentName) { string folderName = "templates"; string webRootPath = _hostingEnvironment.WebRootPath; string newPath = Path.Combine(webRootPath, folderName); if (!Directory.Exists(newPath))// Create New Directory if not exist as per the path { Directory.CreateDirectory(newPath); } var fiName = Guid.NewGuid().ToString() + Path.GetExtension(fil.FileName); using (var fileStream = new FileStream(Path.Combine(newPath, fiName), FileMode.Create)) { fil.CopyTo(fileStream); } // Get uploaded file path with root string fileName = @"wwwroot/" + folderName + "/" + fiName; FileInfo file = new FileInfo(fileName); using (_db) { var document = new Activity_Document(); document.DocumentName = documentName; document.DocumentPath = file.ToString(); document.SubmitionData = DateTime.Now; document.Comments = comments; document.UserId = userId; document.ActivityId = activityId; document.FlagReject = 1; _db.Add(document); _db.SaveChanges(); } return(RedirectToAction("Documents", "Home")); }
public async Task <IActionResult> Create(IFormFile fil, DateTime data, string comments, int userId, int activityId, String DocumentType, String documentName) { string folderName = ""; if (DocumentType.Equals("Ata") || DocumentType.Equals("Ata_Corrigida")) { folderName = "atas"; } else if (DocumentType.Equals("Relatorio")) { folderName = "documentos"; } string webRootPath = _hostingEnvironment.WebRootPath; string newPath = Path.Combine(webRootPath, folderName); if (!Directory.Exists(newPath))// Create New Directory if not exist as per the path { Directory.CreateDirectory(newPath); } var fiName = Guid.NewGuid().ToString() + Path.GetExtension(fil.FileName); using (var fileStream = new FileStream(Path.Combine(newPath, fiName), FileMode.Create)) { fil.CopyTo(fileStream); } // Get uploaded file path with root string fileName = @"wwwroot/" + folderName + "/" + fiName; FileInfo file = new FileInfo(fileName); try { if (DocumentType.Equals("Ata") || DocumentType.Equals("Ata_Corrigida")) { var ata = new Ata(); ata.MeetingDate = data.Date; ata.FilePath = file.ToString(); ata.ActivityId = activityId; if (User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Role).Value != "Aluno") { ata.UserId = userId; } else { ata.StudentId = int.Parse(User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value); } _context.Add(ata); } else { var document = new Activity_Document(); document.DocumentName = documentName; document.DocumentPath = file.ToString(); document.SubmitionData = data; document.Comments = comments; document.UserId = userId; document.ActivityId = activityId; _context.Add(document); } Alert("Documento Adicionado!", "O Documento foi criado com sucesso", NotificationType.success); _context.SaveChanges(); } catch { Alert("Ocorreu um erro", "Não foi possivel adicionar o documento à atividade!", NotificationType.error); } return(RedirectToAction("Details", "Activities", new { id = activityId })); }