Uploading multiple files and creating a Zip File in ASP.NET with C#





5.00/5 (6 votes)
Uploading multiple files and creating a Zip file in ASP.NET with C#.
Introduction
In this example, I will explain how upload multiple files with a limit like 5 or 10 files. The complete validation process we would be going with the Jquery. And how to upload those files onto a folder and also generating a zip for all files particular folder.
Using the code
Here we can see how to use this jQuery plug-in with ASP.NET to upload multiple files. We will also display the information about the files uploaded such as the name, size and type of the file.
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery.MultiFile.js" type="text/javascript"></script>
<asp:FileUpload ID="fuPhoto" runat="server" class="multi" accept="gif|jpg|jpeg" maxlength="5" />
<asp:Button ID="btnZipFile" Text="ZipFile" runat="server" OnClick="btnZipFile_Click" />
<asp:Label ID="myLabel" runat="server" ForeColor="#CC0000" />
<asp:Label ID="lblMsg" runat="server" ForeColor="#CC0000" />
Here you will get to know how to generate a zip file in ASP.NET using C#. To implement the zip file first kindly, upload the Inoc.Zip.dll from DotNetZip library and then attach the same to the bin folder like, go to solution explorer – click on bin – attach the Ionic.zip.dll in the bin folder. And create a new folder named ZipMyFiles in the current project.
Now in this code we are only
uploading with .jpg, .jpeg, .gif type files and zip those files. Here you can
upload any sought of file with extension .jpg, .pdf, .mp3 etc for that you need
to mention the files types on the accept of file upload. Here we are also restricting the number of
files to be uploaded and also ziped with a total of 10 files at a time. Now in the code behind we implement the btnZipFile
click
event ,
HttpFileCollection hfc = Request.Files;
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0)
{
hpf.SaveAs(Server.MapPath("ZipMyFiles") + "\\" +
System.IO.Path.GetFileName(hpf.FileName));
lblMsg.Text += " <br/> <b>File: </b>" + hpf.FileName +
" <b>Size:</b> " + hpf.ContentLength + " <b>Type:</b> " +
hpf.ContentType + " Uploaded Successfully!";
}
}
string NewPath = Server.MapPath("~/ZipMyFiles/");
string[] filenames = Directory.GetFiles(NewPath);
using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
{
string date = DateTime.Now.ToString("d");
date = date.Replace("/", "");
zip.AddFiles(filenames, date);
zip.Save(Server.MapPath(date + ".zip"));
myLabel.Text = "ZIP File Created Successfully";
}
if (myLabel.Text == "ZIP File Created Successfully")
{
Array.ForEach(Directory.GetFiles(NewPath),
delegate(string deleteFile) { File.Delete(deleteFile); });
}
As shown on the code above, the HttpFileCollection
class is
used to retrieve all the files that are uploaded. Files are encoded and
transmitted in the content body using Multipurpose Internet Mail Extensions format
with HTTP content type header. ASP.NET extracts this informaoitn from content
body into individual memberof an HttpFileCollection
.
The HttpPostedFile
class provides methods and properties to
access the contents and properties of each file. Such as, we use this class to
check the length, name and type of the files.
If you want to remove the types of file before clicking on the zip then just click on the cross ‘x’ to remove from the list. Here we had removed 4c96f017-eebb-4d58-a2d6-c446de6534b5_59.jpg file

Error message if you try to select a file apart from the given extensions.
Once you have finally decided upon the files to be zipped, click on the zip button to zip the files to the server. After zip, a message will be displayed to the user as shown,
Kindly check the zip file. And unzip the file and check weather all files.