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
- <asp:GridView ID="griddownload" runat="server" AutoGenerateColumns="false" AlternatingRowStyle-BackColor="#99ccff" HeaderStyle-BackColor="#000099" HeaderStyle-ForeColor="White">
- <Columns>
- <asp:TemplateField HeaderText="check the check box to download the files">
- <ItemTemplate>
- <asp:CheckBox ID="chkselect" runat="server" />
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="File Name">
- <ItemTemplate>
- <asp:Label ID="lblFileName" runat="server" Text='<%# Eval("FileName") %>' />
- <asp:Label ID="lblFilePath" Visible="false" runat="server" Text='<%# Eval("FilePath") %>' />
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- </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.
- private void bindgrid()
- {
- var files = Directory.GetFiles(Server.MapPath("~/Zipped File"));
-
- griddownload.DataSource = from f in files
- select new
- {
- FileName = Path.GetFileName(f),
- FilePath = f
- };
- griddownload.DataBind();
- }
After writing this code call the bindgrid() method at the page load event.
- if (!Page.IsPostBack)
- {
- bindgrid();
- }
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
- private void zippedfile()
- {
- byte[] buffer = new byte[6144];
- var filedownload = Server.MapPath(@"tempfolder/" + Guid.NewGuid().ToString() + ".zip");
-
- var zipOutputStream = new ZipOutputStream(File.Create(filedownload));
- var filePath = String.Empty;
- var fileName = String.Empty;
- var readBytes = 0;
-
- foreach (GridViewRow row in griddownload.Rows)
- {
- var isChecked = (row.FindControl("cboxselect") as CheckBox).Checked;
- if (!isChecked) continue;
- fileName = (row.FindControl("lblfname") as Label).Text;
- filePath = (row.FindControl("lblpath") as Label).Text;
- var addzipfile = new ZipEntry(fileName);
-
- zipOutputStream.PutNextEntry(addzipfile);
-
- using (var fs = File.OpenRead(filePath))
- {
- do
- {
- readBytes = fs.Read(buffer, 0, buffer.Length);
- zipOutputStream.Write(buffer, 0, readBytes);
-
- } while (readBytes > 0);
- }
- }
-
- if (zipOutputStream.Length == 0)
- {
- lblMessage.Text = "First check minimum one check box then download!!!";
- return;
- }
-
- zipOutputStream.Finish();
- zipOutputStream.Close();
- Response.ContentType = "application/x-zip-compressed";
- Response.AppendHeader("Content-Disposition", "attachment; filename=DownloadedFile.zip");
- Response.WriteFile(filedownload);
- Response.Flush();
- Response.Close();
- if (File.Exists(filedownload))
- File.Delete(filedownload);
- }
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.