Multiple files download using ASP.NET 4.5 Framework

Mdjack
Posted by Mdjack under C# category on | Points: 40 | Views : 612
I need to download the selected files into zip file. using the following code i can add the files to zip and downloaded into my local drive.

When I open the / Extract the zip file i am getting the error.

My Code is:

try
{
string downloadFileName = string.Format("YourDownload-{0}.zip", DateTime.Now.ToString("yyyy-MM-dd-HH_mm_ss"));
using (var zipToOpen = new MemoryStream())
{
using (var archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create))
{
foreach (ListItem item in chkFiles.Items)
{
if (item.Selected)
{
var demoFile = archive.CreateEntry(Path.GetFileName(item.Value));

using (var entryStream = demoFile.Open())
{
var data = File.ReadAllBytes(item.Value);
entryStream.Write(data, 0, data.Length);
}
}
}

Response.Clear();
Response.ContentType = "application/zip";
Response.AddHeader("Content-Disposition", "filename=" + downloadFileName);
Response.BinaryWrite(zipToOpen.ToArray());
//Response.OutputStream.Write(zipToOpen.ToArray(), 0, (int)zipToOpen.Length);
}
}
}
catch (Exception ex)
{

throw ex;
}
Response.End();


My total file size is 357KB, but its showing 352KB after downloading. Can anyone please suggest to resolve the issue.

Comments or Responses

Login to post response