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.

Step 2
Select MVC template from the templates window and click OK.

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

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

Step 4
Add class and properties for file information. The following screenshot explains how to add class and class properties. 
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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace MutipleFileDownload.Models {
- public class FileInfo {
- public int FileId {
- get;
- set;
- }
- public string FileName {
- get;
- set;
- }
- public string FilePath {
- get;
- set;
- }
- }
- }
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.

Now, add the code for fetching files. Before adding the code, add “System.IO” namespace.
Coding For Fetching Files
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using MutipleFileDownload;
- using System.IO;
- namespace MutipleFileDownload.Models {
- public class FileDownloads {
- public List < FileInfo > GetFile() {
- List < FileInfo > listFiles = new List < FileInfo > ();
- string fileSavePath = System.Web.Hosting.HostingEnvironment.MapPath("~/Images");
- DirectoryInfo dirInfo = new DirectoryInfo(fileSavePath);
- int i = 0;
- foreach(var item in dirInfo.GetFiles()) {
- listFiles.Add(new FileInfo() {
- FileId = i + 1,
- FileName = item.Name,
- FilePath = dirInfo.FullName + @ "\" + item.Name
- });
- i = i + 1;
- }
- return listFiles;
- }
- }
- }
Next, add action methods in “FileDownloadController”.

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

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

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.
Step 9
Now, add download methods in corresponding Controller. This method is used for merging all the files as a compressed format.
- public ActionResult Download() {
- FileDownloads obj = new FileDownloads();
- //////int CurrentFileID = Convert.ToInt32(FileID);
- var filesCol = obj.GetFile().ToList();
- using(var memoryStream = new MemoryStream()) {
- using(var ziparchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) {
- for (int i = 0; i < filesCol.Count; i++) {
- ziparchive.CreateEntryFromFile(filesCol[i].FilePath, filesCol[i].FileName);
- }
- }
- return File(memoryStream.ToArray(), "application/zip", "Attachments.zip");
- }
- }
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 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
- using MutipleFileDownload;
- using System.IO;
- namespace MutipleFileDownload.Models {
- public class FileDownloads {
- public List < FileInfo > GetFile() {
- List < FileInfo > listFiles = new List < FileInfo > ();
- //Path For download From Network Path.
- string fileSavePath = @ "\\servername\FileFolderName";
- DirectoryInfo dirInfo = new DirectoryInfo(fileSavePath);
- int i = 0;
- foreach(var item in dirInfo.GetFiles()) {
- listFiles.Add(new FileInfo() {
- FileId = i + 1,
- FileName = item.Name,
- FilePath = dirInfo.FullName + @ "\" + item.Name
- });
- i = i + 1;
- }
- return listFiles;
- }
- }
- }
- using MutipleFileDownload.Models;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.IO.Compression;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace MutipleFileDownload.Controllers {
- public class FileDownloadController: Controller {
- // GET: FileDownload
- public ActionResult FileHome() {
- return View();
- }
- public ActionResult Download() {
- FileDownloads obj = new FileDownloads();
- //////int CurrentFileID = Convert.ToInt32(FileID);
- var filesCol = obj.GetFile().ToList();
- using(var memoryStream = new MemoryStream()) {
- using(var ziparchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) {
- for (int i = 0; i < filesCol.Count; i++) {
- ziparchive.CreateEntryFromFile(filesCol[i].FilePath, filesCol[i].FileName);
- }
- }
- return File(memoryStream.ToArray(), "application/zip", "Attachments.zip");
- }
- }
- }
- }
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.

Swati SharmaPosted Jan 24, 2023, 2:05 PM
I used your code but last line return File(memory stream) is showing error.File is non invocable member.I am not able to return.
shubhangi waghmarePosted Aug 11, 2020, 1:40 AM
Application crashed if download size is more than 750mb in my case
Mohamed yasirPosted Jun 14, 2020, 10:41 AM
We need to create zip file based on user input, it will be different values, So I need to pass the value from ajax and download files. Can you please provide solution for these cases?
Oryza AnggaraPosted Dec 10, 2018, 6:07 AM
How to make this into Web API controller?
Pradeep KumarPosted Dec 1, 2018, 4:29 AM
Sir how to save as rar when files coming from server. pls leave code.
shima asadiPosted Aug 2, 2017, 5:00 AM
I can fix my problem by edit this line FilePath = dirInfo.FullName thanks
shima asadiPosted Aug 2, 2017, 4:43 AM
I set permission to everybody in my project folder but did not solved
shima asadiPosted Aug 2, 2017, 4:41 AM
How can solve this problem?UnauthorizedAccessException: Access to the path
shima asadiPosted Aug 2, 2017, 4:40 AM
Ziparchive.CreateEntryFromFile(filesCol[i].FilePath, filesCol[i].FileName);
shima asadiPosted Aug 2, 2017, 4:40 AM
I use this codes but when run i have permission error in this line:
shima asadiPosted Aug 2, 2017, 4:05 AM
Very very thank you.I need this Tutorial.
Delpin Susai RajPosted Aug 28, 2016, 4:22 AM
Nice
Vignesh ManiPosted Aug 22, 2016, 5:16 AM
Thanks To All.
Humayun Kabir MamunPosted Aug 21, 2016, 2:23 AM
Nice share...
Anu VPosted Aug 20, 2016, 1:07 AM
Ni ce..
RajaPosted Aug 20, 2016, 1:03 AM
Good One..
Vignesh ManiPosted Aug 19, 2016, 7:04 AM
Thanks To All.
Hanif HefazPosted Aug 19, 2016, 2:40 AM
Nice one sir, thanks
Hariharan KrishnamoorthiPosted Aug 19, 2016, 2:16 AM
It will be helpful for file handling concepts. Good one Vignesh..
OM RAJANPosted Aug 18, 2016, 7:53 PM
Very nice to learn, thanks vignesh
Ramesh PalaniappanPosted Aug 18, 2016, 5:13 AM
Good One
Ramesh PalaniappanPosted Aug 18, 2016, 5:12 AM
Good One
Manas MohapatraPosted Aug 18, 2016, 2:22 AM
Thanks for sharing. Before we supposed to use Ionic.zip dll or any other third party. Now we can use System.IO.Compression to compress the files.