Download Multiple Files In Compressed Format In ASP.NET MVC 5 Step By Step

Introduction

This article explains how to download multiple files in compressed format using ASP.NET MVC 5. There are several ways to download multiple files in zip format but this article explains the easiest way in a step by step process.

Background

We can download multiple files using zip format, from different sources to the destination locations, such as Server path or Network path. We will see in this article how to download files from server path as well as network path. We need to give permission to the corresponding folder from where we download or upload whether it is from the server or any other network path.

Given below are the steps for downloading multiple files as a zipped file.

Step 1

Go to Visual Studio, open new ASP.NET Web application, and assign a relevant project name. Follow the below screenshot.

new

Step 2

Select MVC template from the templates window and click OK.

MVC template

Step 3

Go to Solution Explorer, right click on Controllers  folder, and add new controller, as shown in the screen below:

controller

The Controller window will open. Select “MVC Controller- Empty” and click Add. Name your controller and click OK again.

controller

Step 4

Add class and properties for file information. The following screenshot explains how to add class and class properties.

Add class

Right click on Models folder, click Add and select the Class option. Finally, give this class a name.

Step 5

Add class properties, after adding class. Class properties help to fetch and store the file details.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace MutipleFileDownload.Models {  
  6.     public class FileInfo {  
  7.         public int FileId {  
  8.             get;  
  9.             set;  
  10.         }  
  11.         public string FileName {  
  12.             get;  
  13.             set;  
  14.         }  
  15.         public string FilePath {  
  16.             get;  
  17.             set;  
  18.         }  
  19.     }  
  20. }  
Step 6

Add another class in Models folder for fetching the files from corresponding locations. We can fetch files from Server path and Network path. Add “FileInfo” name space in “FileDownlod” class.

code
Now, add the code for fetching files. Before adding the code, add “System.IO” namespace.

Coding For Fetching Files
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using MutipleFileDownload;  
  6. using System.IO;  
  7. namespace MutipleFileDownload.Models {  
  8.     public class FileDownloads {  
  9.         public List < FileInfo > GetFile() {  
  10.             List < FileInfo > listFiles = new List < FileInfo > ();  
  11.             string fileSavePath = System.Web.Hosting.HostingEnvironment.MapPath("~/Images");  
  12.             DirectoryInfo dirInfo = new DirectoryInfo(fileSavePath);  
  13.             int i = 0;  
  14.             foreach(var item in dirInfo.GetFiles()) {  
  15.                 listFiles.Add(new FileInfo() {  
  16.                     FileId = i + 1,  
  17.                         FileName = item.Name,  
  18.                         FilePath = dirInfo.FullName + @ "\" + item.Name  
  19.                 });  
  20.                 i = i + 1;  
  21.             }  
  22.             return listFiles;  
  23.         }  
  24.     }  
  25. }  
Step 7

Next, add action methods in “FileDownloadController”.

Add actions methods

Right click on “FileHome” action method and click “Add View“. Then, click “Add” button, as shown in the below screenshot.

Add actions methods

After adding the View page, add the below code for designing the download file page.

code

Step 8

We need two assemblies for compressing many files for download. Without compressing,  we cannot download so many files simultaneously.  So, these two assemblies are: 
  • System.IO.Compression
  • System.IO.Compression.FileSystem

Add two assemblies in our solution, using the following method.

Right click on "Reference" in Solution Explorer. Click “Add Reference” and the Reference Manger window will open. Now, expand Assemblies, select Framework, and find the above mentioned two assemblies. Select those assemblies and finally click OK.

Reference

Step 9

Now, add download methods in corresponding Controller. This method is used for merging all the files as a compressed format.

  1. public ActionResult Download() {  
  2.     FileDownloads obj = new FileDownloads();  
  3.     //////int CurrentFileID = Convert.ToInt32(FileID);  
  4.     var filesCol = obj.GetFile().ToList();  
  5.     using(var memoryStream = new MemoryStream()) {  
  6.         using(var ziparchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) {  
  7.             for (int i = 0; i < filesCol.Count; i++) {  
  8.                 ziparchive.CreateEntryFromFile(filesCol[i].FilePath, filesCol[i].FileName);  
  9.             }  
  10.         }  
  11.         return File(memoryStream.ToArray(), "application/zip""Attachments.zip");  
  12.     }  
  13. }  
Step 10

After adding the above code, just compile your code and run your project. Now, we can download files from the mentioned path from your server.

download files

Download Files from Network Path

We can download files from the network path too. For that, we need to do small changes in “FileDownloads” class. Add the below code for downloading files from network path.

Coding
  1. using MutipleFileDownload;  
  2. using System.IO;  
  3. namespace MutipleFileDownload.Models {  
  4.     public class FileDownloads {  
  5.         public List < FileInfo > GetFile() {  
  6.             List < FileInfo > listFiles = new List < FileInfo > ();  
  7.             //Path For download From Network Path.  
  8.             string fileSavePath = @ "\\servername\FileFolderName";  
  9.             DirectoryInfo dirInfo = new DirectoryInfo(fileSavePath);  
  10.             int i = 0;  
  11.             foreach(var item in dirInfo.GetFiles()) {  
  12.                 listFiles.Add(new FileInfo() {  
  13.                     FileId = i + 1,  
  14.                         FileName = item.Name,  
  15.                         FilePath = dirInfo.FullName + @ "\" + item.Name  
  16.                 });  
  17.                 i = i + 1;  
  18.             }  
  19.             return listFiles;  
  20.         }  
  21.     }  
  22. }  
Controller Coding
  1. using MutipleFileDownload.Models;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.IO;  
  5. using System.IO.Compression;  
  6. using System.Linq;  
  7. using System.Web;  
  8. using System.Web.Mvc;  
  9. namespace MutipleFileDownload.Controllers {  
  10.     public class FileDownloadController: Controller {  
  11.         // GET: FileDownload  
  12.         public ActionResult FileHome() {  
  13.             return View();  
  14.         }  
  15.         public ActionResult Download() {  
  16.             FileDownloads obj = new FileDownloads();  
  17.             //////int CurrentFileID = Convert.ToInt32(FileID);  
  18.             var filesCol = obj.GetFile().ToList();  
  19.             using(var memoryStream = new MemoryStream()) {  
  20.                 using(var ziparchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) {  
  21.                     for (int i = 0; i < filesCol.Count; i++) {  
  22.                         ziparchive.CreateEntryFromFile(filesCol[i].FilePath, filesCol[i].FileName);  
  23.                     }  
  24.                 }  
  25.                 return File(memoryStream.ToArray(), "application/zip""Attachments.zip");  
  26.             }  
  27.         }  
  28.     }  
  29. }  
Conclusion

Thus, this is the procedure of downloading multiple files in  compressed format, in an easy way. This article will help students and those who are newly learning MVC. The next part of this article will explain how to download the latest uploaded files from the server. 


Similar Articles