Download Files In ZIP Format In MVC.NET

Introduction

 
In this article, we will create a demo for copying images from another folder and downloading those images in zip format. We have to follow some simple steps to create a project and export some files in zip format in MVC.NET. 
 
Step 1 - Create a Project
 
After opening Visual Studio, next, we need to create an ASP.NET MVC project. For doing that, just click File - New - Project.
 
 
 
After choosing a project, a new dialog will pop up. In that, select ASP.NET Web Application in the Web tab and give your project the location and a name. Then, click the "Ok" button. Choose the MVC Template.
 
 
Step 2: Install the SharpZipLib Nuget Package 
 
For Installing the SharpZipLib NuGet package, open the NuGet Package Manager from right-clicking on References and select the "Manage NuGet Package" option. Then, perform the following steps.
 
 
Step 3 - Create a Method in Controller for Downloading zip file
 
Write this code into your Controller and give image path into ImageList for images that you want to download in the zip file and create a folder named TempImages in root directory.
 
Right now, I gave the static image path but you can set dynamic image path according to your requirements.
  1. public FileResult DownloadZipFile()  
  2.         {  
  3.   
  4.             var fileName = string.Format("{0}_ImageFiles.zip", DateTime.Today.Date.ToString("dd-MM-yyyy") + "_1");  
  5.             var tempOutPutPath = Server.MapPath(Url.Content("/TempImages/")) + fileName;  
  6.   
  7.             using (ZipOutputStream s = new ZipOutputStream(System.IO.File.Create(tempOutPutPath)))  
  8.             {  
  9.                 s.SetLevel(9); // 0-9, 9 being the highest compression  
  10.   
  11.                 byte[] buffer = new byte[4096];  
  12.   
  13.                 var ImageList = new List<string>();  
  14.   
  15.                 ImageList.Add(Server.MapPath("/Images/01.jpg"));  
  16.                 ImageList.Add(Server.MapPath("/Images/02.jpg"));  
  17.   
  18.   
  19.                 for (int i = 0; i < ImageList.Count; i++)  
  20.                 {  
  21.                     ZipEntry entry = new ZipEntry(Path.GetFileName(ImageList[i]));  
  22.                     entry.DateTime = DateTime.Now;  
  23.                     entry.IsUnicodeText = true;  
  24.                     s.PutNextEntry(entry);  
  25.   
  26.                     using (FileStream fs = System.IO.File.OpenRead(ImageList[i]))  
  27.                     {  
  28.                         int sourceBytes;  
  29.                         do  
  30.                         {  
  31.                             sourceBytes = fs.Read(buffer, 0, buffer.Length);  
  32.                             s.Write(buffer, 0, sourceBytes);  
  33.                         } while (sourceBytes > 0);  
  34.                     }  
  35.                 }  
  36.                 s.Finish();  
  37.                 s.Flush();  
  38.                 s.Close();  
  39.   
  40.             }  
  41.   
  42.             byte[] finalResult = System.IO.File.ReadAllBytes(tempOutPutPath);  
  43.             if (System.IO.File.Exists(tempOutPutPath))  
  44.                 System.IO.File.Delete(tempOutPutPath);  
  45.   
  46.             if (finalResult == null || !finalResult.Any())  
  47.                 throw new Exception(String.Format("No Files found with Image"));  
  48.   
  49.             return File(finalResult, "application/zip", fileName);  
  50.   
  51.         }   
Step 4 - Call the DownloadZipFile() method from the view side
 
In this line, I have set the link named Download zip and called the DownloadZipFile() method.
 
After clicking on it, the zip file will be download. 
  1. <a href="Home/DownloadZipFile/" target="_blank" class="btn btn-primary" style="margin-top:20px;">Download Zip</a>  

Output

 


Similar Articles