我正在尝试在一个压缩文件中导出多个PDF文件。
以前,我曾采取行动来导出一个简单的PDF,它可以工作。但是现在我正尝试压缩报告中的多个文件。
已导出ZIP文件,其中包含正确的文件,但是没有文件可读且大小合适(大于0)。 请问您有一个如何解决此问题的想法? 请在下面找到我在ASP MVC 5 / C#
上为此编写的操作 public ActionResult ExportAllPDF(string month, string year)
{
List<string> lesAffectations = new List<string>(); // sql
var etabs = DataAccessLayer.SGDPDE13ModelInstance.EtablissementsTb.ToList().GetRange(0, 5);
foreach (var item in etabs)
{
lesAffectations.Add(item.ID_Etab);
}
var files = new List<FileStreamResult>();
using (var memoryStream = new MemoryStream())
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
for (int i = 0; i < lesAffectations.Count; i++)
{
//Example: http://localhost:9665/Administrateur/ExportAllPDF?month=10&year=2018
ReportDocument rd = new ReportDocument();
rd.Load(Path.Combine(Server.MapPath("~/Reports"), "ReportTR.rpt"));
var data = GetTraitements(lesAffectations[i], month, year);
DataTable table = new DataTable();
using (var reader = ObjectReader.Create(data))
{
table.Load(reader);
}
rd.SetDataSource(table);
Response.Buffer = false;
Response.ClearContent();
Response.ClearHeaders();
Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
stream.Seek(0, SeekOrigin.Begin);
var fileName = lesAffectations[i] + "_TRAITEMENT_" + month + "_" + year + ".pdf";
files.Add(File(stream, "application/pdf", fileName));
var demoFile = archive.CreateEntry(fileName);
using (var entryStream = demoFile.Open())
using (var streamWriter = new StreamWriter(entryStream))
{
streamWriter.Write(stream);
}
}
}
var zipName = new Random().Next(10000, 9999999);
using (var fileStream = new FileStream(zipName + ".zip", FileMode.Create))
{
var bytes = memoryStream.GetBuffer();
fileStream.Write(bytes, 0, bytes.Length);
}
return File(memoryStream.ToArray(), "application/zip", "Attachments.zip");
}