Reputation: 1395
I have a MemoryStream which represents a zip file. This is how the stream is created :
var zipMemoryStream = new MemoryStream();
using (var zipPackage = Package.Open(zipMemoryStream, FileMode.CreateNew))
{
foreach (var file in files)
{
var c = new Uri("/" + file.FileName + extension, UriKind.Relative);
var zipPart = zipPackage.CreatePart(c, fileType);
if (zipPart == null)
continue;
CopyStream(file.MemoryStream, zipPart.GetStream());
}
}
File.WriteAllBytes("C:\\ZipTest.zip", zipMemoryStream.ToArray());
try
{
mailMsg.Attachments.Add(new Attachment(zipMemoryStream, "ZipTest.zip",
MediaTypeNames.Application.Zip));
}
As you can see I have added the line File.WriteAllBytes which saves the stream to my HDD (for testing).
The file which is saved on the HDD is flawless and weights about 138B if the files in the zip are empty.
However, The attachment I get in the mail weights 0B.
Any idea why the attachment weighs 0B?
Upvotes: 0
Views: 118
Reputation:
Yes, as in most situations like this, the stream's Position is at the end of the stream.
Try
try
{
zipMemoryStream.Position = 0;
mailMsg.Attachments.Add(new Attachment(zipMemoryStream, "ZipTest.zip",
MediaTypeNames.Application.Zip));
}
Upvotes: 1