Download Multiple Files Using Gridview in a Single Zipped Folder

Introduction

In the field of computer science downloading is an important option for users. Every user wants to download data. The data may be in the form of a text file, may be in the form of a video, may be in the form of an audio file but behind all this scenario the most important things is how to download the data without problems. Sometimes we may want to download a file divided into parts and we want to download them but that is very painful for us to download these files one by one and it also takes more time, because we need to many times go to a file and click on it to download them one by one. So to overcome that problem we will learn how to download all the desired files at a single time and also zip them when downloading.

So here we discuss the step-by-step method to download the multiple files and save them in Zipped format, because we know that if we download the data in the zipped format then it is always stored in a single folder and it is in a compressed format so it uses less memory than files downloaded one by one individually.

Step 1

Open your Visual Studio, go to the file menu and select a new empty website.



Step 2

After adding a new website our first task is to download the SharpZipLib. SharpZipLib is nothing special, it is just a .Net API that we can use to zip our files. SharpZipLib is free, you can download it easily from the internet. It is not large, it is just one MB after downloading. Please install it in your system.

Step 3

Then go to your website, right-click on it and add a new Web form to your empty website.



Step 4

After adding the Web form to our website we add a new folder to our website and rename it zipped file or as you want. Here I use the name Zipped File for the folder. This folder contains the files we want to download. This file may be anything, it may be an audio file, a video file, a text file, a Word document, a PDF file or anything else.

Step 5

Now our next step to create a grid view in our web page that we added to our website and bind these files with the grid view to show these files in the grid view. So let see how to bind the files in the grid view available in the zipped fie folder. It is simple, we will see the code and how to do it. See the given code.

Code in the Default.aspx for the grid view

  1. <asp:GridView ID="griddownload" runat="server" AutoGenerateColumns="false" AlternatingRowStyle-BackColor="#99ccff" HeaderStyle-BackColor="#000099" HeaderStyle-ForeColor="White">  
  2.   <Columns>  
  3.   <asp:TemplateField HeaderText="check the check box to download the files">  
  4.   <ItemTemplate>  
  5.   <asp:CheckBox ID="chkselect" runat="server" />  
  6.   </ItemTemplate>  
  7.   </asp:TemplateField>              
  8.   <asp:TemplateField HeaderText="File Name">  
  9.   <ItemTemplate>  
  10.   <asp:Label ID="lblFileName" runat="server" Text='<%# Eval("FileName") %>' />  
  11.   <asp:Label ID="lblFilePath" Visible="false" runat="server" Text='<%# Eval("FilePath") %>' />  
  12.   </ItemTemplate>  
  13.   </asp:TemplateField>  
  14.   </Columns>  
  15.   </asp:GridView>  
Step 6

Now we bind the grid view from the Zipped File items and show these items in the grid view with the checked box.

Code in the Default.aspx.cs

Using this code we bind the grid view.
  1. private void bindgrid()  
  2.   {  
  3.       var files = Directory.GetFiles(Server.MapPath("~/Zipped File"));  
  4.   
  5.       griddownload.DataSource = from f in files  
  6.                            select new  
  7.                            {  
  8.                                FileName = Path.GetFileName(f),  
  9.                                FilePath = f  
  10.                            };  
  11.       griddownload.DataBind();  
  12.   }  
After writing this code call the bindgrid() method at the page load event.
  1. if (!Page.IsPostBack)  
  2.         {  
  3.             bindgrid();  
  4.         }  
Now when you run your application you will see the output window as in the following:



As you see in the output window there are two columns, one column has the check boxes and the other column has the file names to be saved in the Zipped File folder depending on your requirements.

Step 7

Now we add a button and a label. The button is for selecting the files using check boxes. Click on it then the downloading will start and the label is used to show the message or warning if you did not choose any file and click on the download button.

Step 8

Now we make a method by which we select the file from the Zipped File folder one by one and then when the user clicks on the download button the selected files will be downloaded in a folder in the zipped format. So now we make a folder named the extra file in our website, in this folder we first make the files temporarily zipped. We select a random name to save the temporary zipped file in our folder.

Step 9

The method through which we select the file and save them into the tempfolder is given below.

Code for method
  1. private void zippedfile()  
  2.     {  
  3.         byte[] buffer = new byte[6144];  
  4.         var filedownload = Server.MapPath(@"tempfolder/" + Guid.NewGuid().ToString() + ".zip");  
  5.   
  6.         var zipOutputStream = new ZipOutputStream(File.Create(filedownload));  
  7.         var filePath = String.Empty;  
  8.         var fileName = String.Empty;  
  9.         var readBytes = 0;  
  10.   
  11.         foreach (GridViewRow row in griddownload.Rows)  
  12.         {  
  13.             var isChecked = (row.FindControl("cboxselect"as CheckBox).Checked;  
  14.             if (!isChecked) continue;  
  15.             fileName = (row.FindControl("lblfname"as Label).Text;  
  16.             filePath = (row.FindControl("lblpath"as Label).Text;  
  17.             var addzipfile = new ZipEntry(fileName);  
  18.   
  19.             zipOutputStream.PutNextEntry(addzipfile);  
  20.   
  21.             using (var fs = File.OpenRead(filePath))  
  22.             {  
  23.                 do  
  24.                 {  
  25.                     readBytes = fs.Read(buffer, 0, buffer.Length);  
  26.                     zipOutputStream.Write(buffer, 0, readBytes);  
  27.   
  28.                 } while (readBytes > 0);  
  29.             }  
  30.         }  
  31.   
  32.         if (zipOutputStream.Length == 0)  
  33.         {  
  34.             lblMessage.Text = "First check minimum one check box then download!!!";  
  35.             return;  
  36.         }  
  37.   
  38.         zipOutputStream.Finish();  
  39.         zipOutputStream.Close();  
  40.         Response.ContentType = "application/x-zip-compressed";  
  41.         Response.AppendHeader("Content-Disposition""attachment; filename=DownloadedFile.zip");  
  42.         Response.WriteFile(filedownload);  
  43.         Response.Flush();  
  44.         Response.Close();  
  45.         if (File.Exists(filedownload))  
  46.             File.Delete(filedownload);  
  47.     }  
Step 10

After writing this method call this method on the button click event. Then when you run your application you will see output like this.



Now here we selected six files related to different categories so when we clicked on the download button all six files will be downloaded at a single time in the zipped folder named Downloaded file and when we open this folder then all six files are there. So this the procedure to download multiple files using a grid view in a Zip folder.

If you do not select any file, in other words you do not check any check boxes and click on the download button then there will be a warning message generated.



Summary

In this article we learned how to download multiple files in a single Zip folder using a grid view. This is a very good option for downloading the files because if there are many files and we want to download them so no user wants to always do an individual file then he/she want to do a download of these files in a single click then this a good technique to do a single-click operation for downloading. As we know here we download the files as a zipped format so it is also saves memory and increases the download speed. I hope this article will be useful for readers for making a grid view through which we download multiple files in zipped format.