How to Set Multifile Download in Server Folder in Asp.Net Using CheckboxList



Here in this Post we will show how to Set Download Multiple File from Server Folder Using Checkbox List as ZIP file. You can Download Multiple file from server Whatever Folder Set for Download in Asp.Net. So let’s start.

Create New Webpage Drag and drop Checkbox list Control and button Control in Webpage.

<div>
 <asp:CheckBoxList ID="chkAlbum" runat="server">
 </asp:CheckBoxList>
 <asp:Button ID="btnDownload" runat="server" OnClick="btnDownload_Click" Text="Download as zip file" />
</div>

Now create one Folder in Web Application and Add Some File in that Folder so we can set for Download.

Now Go to Code behind File and Code for Download and if File is Selected then That file only download from Server.

In Page_Load () event we will check for Checked Item and set for Download and Store That Item in Checkbox list.

protected void Page_Load(object sender, EventArgs e)
{
   string[] files = Directory.GetFiles(Server.MapPath(".") + @"\Album\Jokers");
   foreach (string file in files)
   {
      chkAlbum.Items.Add(new ListItem(Path.GetFileName(file), file));
   }
}

Now On Button Click event We Will See for how many Item Selected and Which Item user want to download from Server. So File download from Server as Zip file and asked for save.

protected void btnDownload_Click(object sender, EventArgs e)
{
   Response.Clear();
   Response.ContentType = "application/zip";
   Response.AddHeader("content-disposition", "filename=" + "Donload.zip");
   using (ZipFile zip = new ZipFile())
   {
     foreach (ListItem item in chkAlbum.Items)
     {
        if (item.Selected)
        {
          zip.AddFile(item.Value, "Album");
        }
     }
   zip.Save(Response.OutputStream);
   }
}

Now run your Web Application in Browser and Check for Output.

Now Select Multiple item for Download and Click on Download as zip file for Download.

Related Posts

Previous
Next Post »

Thanks for comments.....