How can download multiple files at once in such as as zip file, below is the code : which display all documents (it may be 2 documents or more) and i want one button on that button it download all documents,
How can i do?
How can download multiple files at once in such as as zip file, below is the code : which display all documents (it may be 2 documents or more) and i want one button on that button it download all documents,
How can i do?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vishal JoshiRank #377 · 6.7K reputationPosted Feb 2, 2024, 6:12 AM EST
Hello Garima,
Try the below code to download multiple files.
public void DownloadFilesAsZip(decimal applnno) { using (MemoryStream memoryStream = new MemoryStream()) { // Create a ZipArchive instance using the memory stream using (ZipArchive zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) { Linedeptentity entity = new Linedeptentity(); // Get Files data from the database var filesData = entity.getdeptenclosure(applnno); // Add files to the ZipArchive from the specified file paths foreach (filedataModel filedata in filesData) { string FileName = filedataModel.FileName; Stream stream = new MemoryStream(filedataModel.Document); var zipEntry = zipArchive.CreateEntry(FileName); using (var entryStream = zipEntry.Open()) stream.CopyTo(entryStream); } } // Set up the HTTP response for downloading the zip file Response.Clear(); Response.BufferOutput = false; Response.ContentType = "application/zip"; Response.AddHeader("content-disposition", "attachment; filename=download.zip"); // Write the contents of the memory stream to the HTTP response stream memoryStream.Seek(0, SeekOrigin.Begin); memoryStream.CopyTo(Response.OutputStream); Response.Flush(); Response.End(); } }Model Class
public class filedataModel { public string FileName { get; set; } public byte[] Document { get; set; } }Thanks
Vishal Joshi
Jayraj ChhayaRank #304 · 8.2K reputationPosted Feb 1, 2024, 12:54 AM EST
To download multiple files as a zip in Dot net MVC, you can follow these steps:
First, make sure you have the
System.IO.CompressionandSystem.IO.Compression.FileSystemnamespaces imported in your project.Create a method that takes the file paths of the documents you want to download as parameters. This method will create a zip file and add the documents to it. Here's an example:
using System.IO; using System.IO.Compression; public void DownloadFilesAsZip(string[] filePaths) { string zipFileName = "Documents.zip"; using (ZipArchive zipArchive = ZipFile.Open(zipFileName, ZipArchiveMode.Create)) { foreach (string filePath in filePaths) { zipArchive.CreateEntryFromFile(filePath, Path.GetFileName(filePath)); } } // Return the zip file for download Response.ContentType = "application/zip"; Response.Headers.Add("Content-Disposition", "attachment; filename=" + zipFileName); Response.TransmitFile(zipFileName); Response.Flush(); Response.End(); }DownloadFilesAsZipmethod and pass the file paths of the documents you want to download. Here's an example:Make sure to replace
"ControllerName"with the actual name of your controller.That's it! When the user clicks the "Download All Documents" button, the files will be downloaded as a zip file.
Amit MohantyRank #112 · 20K reputationPosted Feb 1, 2024, 2:27 AM EST
Create a model to store information about each file, such as file name, path, and content type.
public class FileModel { public string FileName { get; set; } public string FilePath { get; set; } public string ContentType { get; set; } }In your controller, create an action that prepares the files you want to include in the zip file and make sure you have the
System.IO.Compressionnamespace included in your project.using System.IO.Compression; public class DownloadController : Controller { public ActionResult DownloadFiles() { // Download all files from a provided folder path var folderPath = Server.MapPath("/Articles/"); // Ensure the folder exists if (!Directory.Exists(folderPath)) return HttpNotFound("Folder not found."); var files = Directory.GetFiles(folderPath); if (files.Length == 0) return HttpNotFound("No files found to download."); List fileList = files.Select(file => { var fileInfo = new FileInfo(file); return new FileModel { FileName = fileInfo.Name, FilePath = file, ContentType = "text/plain" }; }).ToList(); // If you have the files details separately //List fileList = new List //{ // new FileModel { FileName = "Document1.txt", FilePath = "path/to/Document1.txt", ContentType = "text/plain" }, // new FileModel { FileName = "Document2.txt", FilePath = "path/to/Document2.txt", ContentType = "text/plain" } // // Add more files as needed //}; return ZipFiles(fileList); } private ActionResult ZipFiles(List files) { using (MemoryStream ms = new MemoryStream()) { using (ZipArchive archive = new ZipArchive(ms, ZipArchiveMode.Create, true)) { foreach (var file in files) { ZipArchiveEntry entry = archive.CreateEntry(file.FileName); using (Stream entryStream = entry.Open()) using (FileStream fileStream = System.IO.File.OpenRead(file.FilePath)) { fileStream.CopyTo(entryStream); } } } return File(ms.ToArray(), "application/zip", "Documents.zip"); } } }In your view, create a button that triggers the download.
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 2, 2024, 9:20 AM EST
To download multiple files as a single zip file in response to a button click, you can follow these general steps using a combination of server-side and client-side code. Below is a basic example using ASP.NET and C#.
protected void DownloadButton_Click(object sender, EventArgs e) { // Create a unique temporary folder for storing the individual files string tempFolder = Server.MapPath("~/TempDownloads/" + Guid.NewGuid().ToString()); Directory.CreateDirectory(tempFolder); try { // Copy each file to the temporary folder foreach (string filePath in filePathsList) { string fileName = Path.GetFileName(filePath); string destinationPath = Path.Combine(tempFolder, fileName); File.Copy(filePath, destinationPath); } // Create a zip file containing all files in the temporary folder string zipFileName = "DownloadedFiles.zip"; string zipFilePath = Server.MapPath("~/TempDownloads/" + zipFileName); ZipFile.CreateFromDirectory(tempFolder, zipFilePath); // Send the zip file to the client for download Response.Clear(); Response.ContentType = "application/zip"; Response.AppendHeader("Content-Disposition", "attachment; filename=" + zipFileName); Response.TransmitFile(zipFilePath); Response.End(); } finally { // Clean up: Delete the temporary folder and its contents Directory.Delete(tempFolder, true); } }