I have a program which needs to download multiple files at once. I can download a single file by using this single file download, but it doesn't work for multiple.
How can one download multiple files at once in such as as zip file?
You need to pack files and write a result to a response. You can use SharpZipLib compression library.
Code example:
Response.AddHeader("Content-Disposition", "attachment; filename=" + compressedFileName + ".zip");
Response.ContentType = "application/zip";
using (var zipStream = new ZipOutputStream(Response.OutputStream))
{
foreach (string filePath in filePaths)
{
byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
var fileEntry = new ZipEntry(Path.GetFileName(filePath))
{
Size = fileBytes.Length
};
zipStream.PutNextEntry(fileEntry);
zipStream.Write(fileBytes, 0, fileBytes.Length);
}
zipStream.Flush();
zipStream.Close();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With