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;
- }
- }
- }
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.
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;
- }
- }
- }
Step 7
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();
-
- 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");
- }
- }
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 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 > ();
-
- 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;
- }
- }
- }
Controller Coding
- 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 {
-
- public ActionResult FileHome() {
- return View();
- }
- public ActionResult Download() {
- FileDownloads obj = new FileDownloads();
-
- 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");
- }
- }
- }
- }
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.