For .Net Framework 4.0 and bellow there is no inbuilt zipping functionality. 3rd party libraries like DotNetZip, SharpZipLib are playing a great role in this scenario. But if you already use Telerik custom controls for your other developments on a project then you can easily use Telerik ZipPackage to crate zip files.
using Telerik.Web.Zip;
using System.IO;
.....
...
..
Private void GeneratePdfFiles()
{
...//report parameters and data source credentials
MemoryStream memoryStream = new MemoryStream();
ZipPackage Package = ZipPackage.Create(memoryStream);
Stream stream = default(Stream);
foreach(reportParameters in reportParameterCollection)
{
byte[] reportByte= RenderReport(reportServerUrl, reportParameters, reportPath, dataSourceCredentials, networkCredential, "pdf");
stream = new MemoryStream(reportByte);
Package.AddStream(stream, string.Format("ReportName.pdf"), Telerik.Web.Zip.CompressionType.Default);
}
SendZipToClient(memoryStream , Package);
}
private void SendZipToClient(MemoryStream memStream, ZipPackage Package)
{
Package.Close(false);
memStream.Position = 0;
if (memStream != null && memStream.Length > 0)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=FormB.zip");
Response.ContentType = "application/zip";
Response.BufferOutput = false; // to prevent buffering
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = memStream.Read(buffer, 0, buffer.Length)) > 0)
{
Response.OutputStream.Write(buffer, 0, bytesRead);
}
Response.End();
}
}
No comments:
Post a Comment