Search code examples
c#asp.net-coreasp.net-core-mvc

Write data from `FileContentResult`


I'm processing multiple pdf files, so data from each file is stored in a list of FileContentResult. I can't figure out how to write this data into pdf files again. My work is as follows:

[HttpPost]
public ActionResult Create(Job newJob)
{
    string path = MyServer.MapPath(@"\Files\");
    DirectoryInfo d = new DirectoryInfo(path);
    List<FileContentResult> fileContentResults = new List<FileContentResult>();
    foreach (var file in d.GetFiles("*.pdf"))
    {
        if (System.IO.File.Exists(file.FullName))
        {
            var sourceFileStream = System.IO.File.OpenRead(file.FullName); 
            var outputStream = new MemoryStream(); //make memorystream
            var pdf = new PdfDocument(new PdfReader(sourceFileStream), new PdfWriter(outputStream)); 
            PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, false); // create pdf document with form
            IDictionary<String, PdfFormField> fields = form.GetAllFormFields();
            if (fields != null)
            {
                //Populate fields...
            }
                pdf.Close();
                byte[] bytes = outputStream.ToArray();
                fileContentResults.Add(File(bytes, System.Net.Mime.MediaTypeNames.Application.Octet, file.FullName));
            }
        }
    }
    foreach (var file in fileContentResults)
    {
        //I do not know how to write from FileContentResult(file) to a pdf file and add this pdf file to a zip folder
    }
    return View();
}

I cant figure out how to write data from FileContentResult to create pdf file and then add this file to a zip folder. I've tried looking on the web but couldn't find anything very helpful.


Solution

  • string zipPath = MyServer.MapPath(@"\Files\MyZip.zip"); // Path for the zip file
    using (var zipArchive = new FileStream(zipPath, FileMode.Create))
    {
        using (var archive = new System.IO.Compression.ZipArchive(zipArchive, ZipArchiveMode.Create))
        {
            foreach (var fileContent in fileContentResults)
            {
                string pdfFileName = Path.GetFileName(fileContent.FileDownloadName); 
                string pdfFilePath = MyServer.MapPath($@"\Files\{pdfFileName}"); 
    
                // Write the PDF file
                //System.IO.File.WriteAllBytes(pdfFilePath, fileContent.FileContents);
    
                using (var fileStream = new FileStream(pdfFilePath, FileMode.Create, FileAccess.Write, FileShare.Read))
                {
                    fileStream.Write(fileContent.FileContents, 0, fileContent.FileContents.Length);
                }
                archive.CreateEntryFromFile(pdfFilePath, pdfFileName);
            }
        }
    }
    
    // Return the ZIP file as a download (if needed)
    byte[] zipBytes = System.IO.File.ReadAllBytes(zipPath);
    return File(zipBytes, "application/zip", "MyZip.zip");
    

    first write the byte array from each FileContentResult to individual PDF files on the server. Then, use System.IO.Compression.ZipArchive to create a ZIP file and add all the newly created PDF files to this ZIP archive. This approach will allow you to convert your list of FileContentResult objects into a single downloadable ZIP file containing all the PDFs.