我正在尝试创建一个DotNetZip压缩文件,并将其流式传输给用户。在压缩文件中,我插入了两个内存流。但是出现了一个问题,当我打开压缩文件时,它是空的。我查看了文档,没有找到什么有用的信息。以下是我的代码:
public async Task<FileResult> DownloadFile(string fileName){
//Create image memory stream
System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath("~/Images/" + fileName));
MemoryStream msImage = new MemoryStream();
image.Save(msImage, image.RawFormat);
//Create Word document using DocX
MemoryStream msDoc = new MemoryStream();
DocX doc = DocX.Create(msDoc);
Paragraph p1 = doc.InsertParagraph();
p1.AppendLine("Text information...");
Paragraph p2 = doc.InsertParagraph();
p2.AppendLine("DISCLAIMER: ...");
doc.SaveAs(msDoc);
//Create Zip File and stream it to the user
MemoryStream msZip = new MemoryStream();
ZipFile zip = new ZipFile();
msImage.Seek(0, SeekOrigin.Begin);
msDoc.Seek(0, SeekOrigin.Begin);
ZipEntry imageEntry = zip.AddEntry("MyImageName.jpg", msImage);
ZipEntry docEntry = zip.AddEntry("MyWordDocName.docx", msDoc);
zip.Save(msZip);
image.Dispose();
doc.Dispose();
zip.Dispose();
return File(msZip, System.Net.Mime.MediaTypeNames.Application.Octet, "MyZipFileName.zip");
}
我已经检查了msImage和msDoc的大小,它们都加载了数据,但是msZip的大小非常小。 更不用说当它下载时,它是一个空的zip文件。 我需要知道为什么这些流没有被添加。 非常感谢!