Click here to Skip to main content
13,147,689 members (31,050 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 18 Jul 2013

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

, 18 Jul 2013
Rate this:
Please Sign up or sign in to vote.
Uploading multiple files and creating a Zip file in ASP.NET with C#.


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 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") + "\\" +
        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")
      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.

Click on the browse button to upload a file, one at a time. To upload more than one file click on the browse button again and select the file you would like to upload. Here in our case I had restricted with total of 10 files and with extension of .jpg/.jpeg/.gif files only allowed if you try to select apart from these extensions you will get a popup message.

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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mannava Siva Aditya
Web Developer
India India
I am a 29 year old software Web Developer from Hyderabad, India. I have been working since approximately age 25. Where as in IT Development industry since 27. I am Microsoft Certified Technology Specialist.

I have taught myself in development, beginning with Microsoft's technologies ASP.NET, Approximately 3 years ago, I was given an opportunity to work as a freelance in the tech field. Now I am working as a web developer where my roles make me purely in web based technology solutions which manage and control access to applications and patient information stored in legacy systems, client-server applications.

I too had an opportunity to train some IT professionals with technical skills in development area. Which became my passion.

I have worked on various .NET framework versions(2.0 , 3.5, 4.0) and have been learning every new technology being introduced. Currently, I am looking forward to working in R & D in .Net to create distributed, reusable applications.

You may also be interested in...


Comments and Discussions

GeneralCan u share this example files? Pin
muhammet safa3-Jun-15 23:09
membermuhammet safa3-Jun-15 23:09 
QuestionSource? Pin
SuNNySiDeDowN16-May-14 11:47
memberSuNNySiDeDowN16-May-14 11:47 
AnswerRe: Source? Pin
Mannava Siva Aditya16-Jun-14 20:10
memberMannava Siva Aditya16-Jun-14 20:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 18 Jul 2013
Article Copyright 2013 by Mannava Siva Aditya
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid