I'm using this solution to download files, zip and send to user, (Azure blobs).
This is what I have came up with so far:
public void DownloadImages(DownloadImagesModel model)
{
if (ModelState.IsValid && model.ImageIDs != null && model.ImageIDs.Count > 0)
{
var currentUser = _claimService.GetCurrentClaimsIdentity(TimeZoneExtensions.GetCurrentDate());
var images = _fileService.GetImages(currentUser.CompanyID, model.ImageIDs).ToList();
if (images != null && images.Count > 0)
{
string imageContainerURL = _fileService.GetImageContainerURL(currentUser.CompanyID) + currentUser.ImageContainerToken;
using (var zipOutputStream = new ZipOutputStream(Response.OutputStream))
{
zipOutputStream.SetLevel(0); // 0 - store only to 9 - means best compression
Response.BufferOutput = false;
Response.AddHeader("Content-Disposition", "attachment; filename=" + "zipFileName.rar");
Response.ContentType = "application/octet-stream";
foreach (var image in images)
{
if (image.ImageLinks != null)
{
List<ImageLinkModel> imageLinks = new List<ImageLinkModel>();
if (model.FormatID == 0)
imageLinks = image.ImageLinks.ToList();
else
imageLinks = image.ImageLinks.Where(m => m.FormatID == model.FormatID).ToList();
if (imageLinks != null && imageLinks.Count > 0)
{
//Every image has multiple blobs for different sizes
foreach (var link in imageLinks)
{
CloudBlockBlob blob = _fileService.GetBlob(imageContainerURL, link.BlobName);
var entry = new ZipEntry(image.ImageName)
{
DateTime = TimeZoneExtensions.GetCurrentDate(),
Size = blob.Properties.Length
};
zipOutputStream.PutNextEntry(entry);
//Download
blob.DownloadToStream(zipOutputStream);
Response.Flush();
if (!Response.IsClientConnected)
{
break;
}
}
}
}
}
zipOutputStream.Finish();
zipOutputStream.Close();
}
Response.End();
}
}
}
When I open the downloaded zip file I get:
Unexpected end of archive.
The zip should contain three files, but only contains one. The size is also 0..
What have I missed?
Edit 1
Should I specify .rar type when naming?
Don't I need to set position for stream?
Have I positioned Flush, Finish, Close and End functions correctly?
Edit 2
This code should work!?
public ActionResult DownloadImages(DownloadImagesModel model)
{
if (ModelState.IsValid && model.ImageIDs != null && model.ImageIDs.Count > 0)
{
var currentUser = _claimService.GetCurrentClaimsIdentity(TimeZoneExtensions.GetCurrentDate());
var images = _fileService.GetImages(currentUser.CompanyID, model.ImageIDs).ToList();
if (images != null && images.Count > 0)
{
var cloudStorageAccount = new CloudStorageAccount(new StorageCredentials("name", "pw"), true);
var container = cloudStorageAccount.CreateCloudBlobClient().GetContainerReference("images-1");
using (var zipOutputStream = new ZipOutputStream(Response.OutputStream))
{
foreach (var image in images)
{
if (image.ImageLinks != null)
{
List<ImageLinkModel> imageLinks = new List<ImageLinkModel>();
if (model.FormatID == 0)
imageLinks = image.ImageLinks.ToList();
else
imageLinks = image.ImageLinks.Where(m => m.FormatID == model.FormatID).ToList();
if (imageLinks != null && imageLinks.Count > 0)
{
zipOutputStream.SetLevel(0);
var blob = container.GetBlockBlobReference(imageLinks.First().BlobName);
var entry = new ZipEntry(imageLinks.First().BlobName);
zipOutputStream.PutNextEntry(entry);
//Download
blob.DownloadToStream(zipOutputStream);
}
}
}
zipOutputStream.Finish();
zipOutputStream.Close();
}
Response.BufferOutput = false;
Response.AddHeader("Content-Disposition", "attachment; filename=" + "zipFileName.zip");
Response.ContentType = "application/octet-stream";
Response.Flush();
Response.End();
return null;
}
}
return null;
}