Reputation: 589
I'm trying to download zip file. But downloaded zip file is damaged or corrupted and I can't open it.
I use Ionic.Zip
library for create my File.zip
. My code:
public static Stream LoadImages(int[] ids)
{
var images = new List<byte[]>();
var imgNames = new List<string>();
foreach (var id in ids)
{
string fName;
images.Add(LoadImage(id, out fName));
imgNames.Add(fName);
}
MemoryStream outputStream = new MemoryStream();
using (var zip = new Ionic.Zip.ZipFile())
{
for(int i = 0; i < images.Count; i++)
{
zip.AddEntry(imgNames[i], images[i]);
}
zip.Save(outputStream);
}
return outputStream;
}
And my Controller action:
public FileResult DownloadGallery(int[] ids)
{
var stream = ImageManager.LoadImages(ids);
return File(stream, "application/zip", "gallery.zip");
}
Maybe my zip file
incorrect or problem in my Http Response
...
Have you any ideas?
Upvotes: 1
Views: 1229
Reputation: 141638
You need to rewind your MemoryStream
back to the beginning:
public FileResult DownloadGallery(int[] ids)
{
var stream = ImageManager.LoadImages(ids);
stream.Position = 0;
return File(stream, "application/zip", "gallery.zip");
}
The FileResult
only starts writing from the stream where ever its current position is.
Upvotes: 3