Wednesday 28 November 2018

Download DataList checkbox checked file in zip using Ionic.zip dll in asp.net

//////////////////// HTML

 using Ionic.Zip; namespace


<asp:DataList ID="DL_FileManagement" runat="server" DataKeyField="FileNames" RepeatDirection="Horizontal" RepeatLayout="Flow" CssClass="row col-lg-12"
CellPadding="2" CellSpacing="5">
   <ItemStyle ForeColor="Black" />
         <ItemTemplate>
           <div class="col-lg-2 p-0">
               <asp:CheckBox runat="server" ID="Chk_TabtoDownload" Style="float: left; margin-top: 6px; margin-right: -8px;" CssClass="pull-right" Text="&nbsp;" />

                  </div>
                       </ItemTemplate>
    </asp:DataList>


button

<asp:LinkButton runat="server" ID="lbtn_FMDataBulk" OnClick="lbtn_FMDataBulk_OnClick">
                                                     <i class="glyphicon glyphicon-download btn-success" style="font-size:15px; padding:5px;"></i>
                                                                </asp:LinkButton>




//////////////////////////////.cs



protected void Download_Files_Zip(List<string> filenames, string FolderName)
    {
        try
        {
            string fullPath = ResolveUrl("~/" + FolderName + "/");
            string chkFile = Server.MapPath("~/" + FolderName + "/");

            using (ZipFile zip = new ZipFile())
            {
                zip.AlternateEncodingUsage = ZipOption.AsNecessary;
                zip.AddDirectoryByName("Files");
                for (int i = 0; i < filenames.Count; i++)
                {
                    string Fpaths = chkFile + Convert.ToString(filenames[i]);
                    FileInfo fi = new FileInfo(Fpaths);
                    if (fi.Exists)
                    {
                        string filePath = fullPath + Convert.ToString(filenames[i]);
                        zip.AddFile(filePath, "Files");
                    }
                }
                Response.Clear();
                Response.BufferOutput = false;
                string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyyMMMddHHmmss"));
                Response.ContentType = "application/zip";
                Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
                zip.Save(Response.OutputStream);
                Response.End();
            }
        }
        catch (Exception)
        {
        }
    }

    protected void lbtn_FMDataBulk_OnClick(object sender, EventArgs e)
    {
        try
        {
            int i = 0;
            string[] files = new string[] { };
            List<string> filesNames = new List<string>();
            foreach (DataListItem item in DL_FileManagement.Items)
            {
                if ((item.FindControl("Chk_TabtoDownload") as CheckBox).Checked)
                {
                    filesNames.Add((item.FindControl("lbl_FilesNames") as Label).Text);
                    i++;
                }
            }
            Download_Files_Zip(filesNames, "Document");
        }
        catch (Exception)
        {
        }
    }


No comments:

Post a Comment

Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...