Why didn’t you close the MemoryStream, I would wrap this in a using clause, can the same be said about zipFile ? Also, dt I assume this is a DataTable ... puts in error checking to see if there are any rows, see the code below ...
dt.TableName = "Declaration";
if (dt.Rows! = null && dt.Rows.Count> = 1) {
using (MemoryStream stream = new MemoryStream ()) {
dt.WriteXml (stream);
// Thanks Cheeso / Mikael
stream.Seek (0, SeekOrigin.Begin);
//
using (ZipFile zipFile = new ZipFile ()) {
zipFile.AddEntry ("Report.xml", "", stream);
Response.ClearContent ();
Response.ClearHeaders ();
Response.AppendHeader ("content-disposition", "attachment; filename = Report.zip");
//zipFile.Save(Response.OutputStream);
zipFile.Save (stream);
// Commented this out
/ *
Response.Write (zipstream); // <----- Where did that come from?
* /
}
Response.Write (stream);
}
}
// No rows ... don't bother ...
Edit: After looking at it again and realizing that Ionic.Ziplib from Codeplex was used, I changed the code a bit, instead of zipFile.Save(Response.OutputStream); I used zipFile.Save(stream); using the stream instance of the MemoryStream class and wrote it out using Response.Write(stream); .
Edit # 2: Thanks to Cheeso + Mikael for pointing out an obvious flaw - I missed it in a mile and didn't understand their comment until I realized that the stream was at the end ...
Hope this helps, Regards, Tom.
t0mm13b Feb 15 2018-10-15 14:04
source share