public IActionResult StartTicketDownload(Vereniging vereniging, TicketsDownloadViewModel vm) { McEditie editie = _editieRepository.GetNextOrCurrentEditieIncludeTickets(); if (editie == null) { return(NotFound("Er is nog geen opkomende editie aangemaakt, als dit volgens u niet klopt, gelieve dan contact op te nemen met [email protected]")); } string blankPDFpath; string ticketPath; try { string blankPDFname = _config.GetValue <string>("EmptyTicketName"); ticketPath = Path.Combine(Directory.GetCurrentDirectory(), "Tickets"); blankPDFpath = Path.Combine(Directory.GetCurrentDirectory(), "Tickets", blankPDFname); } catch (Exception ex) { TempData["error"] = $"Oeps er ging iets mis, indien dit blijft voorkomen, stuur dan volgende tekst door naar [email protected]: {ex.Message}"; return(RedirectToAction(nameof(Index))); } if (!editie.VerenigingHasTickets(vereniging)) { TempData["error"] = "Sorry, u heeft geen tickets..."; return(RedirectToAction(nameof(Index))); } //make newlines also possible string memberNamesString = vm.MemberNames?.Replace(Environment.NewLine, ";"); List <string> memberNames = memberNamesString?.Split(";").ToList(); editie.GenerateTicketsForVereniging(vereniging, blankPDFpath, ticketPath, memberNames); _editieRepository.SaveChanges(); using (MemoryStream ms = new MemoryStream()) { using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true)) { foreach (Ticket t in editie.GetAllTicketsOfVereniging(vereniging)) { var ZipArchiveEntry = archive.CreateEntry(t.TicketName, CompressionLevel.Fastest); byte[] ticketBytes = System.IO.File.ReadAllBytes(t.PdfPath); using (var zipStream = ZipArchiveEntry.Open()) zipStream.Write(ticketBytes, 0, ticketBytes.Length); } } Task.Run(() => editie.RemoveTicketsVereniging(vereniging, ticketPath)); return(File(ms.ToArray(), "application/zip", "Tickets.zip")); } }