During project implementation, You Can Package Multiple files together and provide them to users for download. For example, in an ASP. NET development project, you can use a gridview to select the checkbox of the corresponding row data to package and download the selected file.
Dotnetzip is used to compress and Package Multiple files. You need to download the file at http://dotnetzip.codeplex.com/, which is immediately available.
Let's take a look at the following:
Default. aspx:
<% @ Page Language = "C #" codefile = "default. aspx. cs" inherits = "_ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> package and download images </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<P>
<Asp: button id = "packdown" runat = "server" text = "package download" onclick = "packdown_click"/> </P>
<Asp: gridview id = "gridview1" runat = "server" width = "500px"
Cellpadding = "8" cellspacing = "1">
<Columns>
<Asp: templatefield headertext = "& lt; input type = & quot; checkbox & quot;/& gt;" insertvisible = "false">
<Itemtemplate>
<Asp: checkbox id = "checkbox1" runat = "server"/>
</Itemtemplate>
<Itemstyle horizontalalign = "center"/>
</ASP: templatefield>
<Asp: templatefield headertext = "file list" insertvisible = "false">
<Edititemtemplate>
<Asp: Label id = "label1" runat = "server" text = '<% # eval_r ("name") %>'> </ASP: Label>
</Edititemtemplate>
<Itemtemplate>
<Asp: Label id = "label1" runat = "server" text = '<% # BIND ("name") %>'> </ASP: Label>
</Itemtemplate>
</ASP: templatefield>
</Columns>
</ASP: gridview>
</Form>
</Body>
</Html>
Default. aspx. CS:
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. IO;
Using ionic. Zip;
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
Bindfileslist ();
}
}
Void bindfileslist ()
{
List <system. Io. fileinfo> lstfi = new list <system. Io. fileinfo> ();
String [] files = system. Io. Directory. getfiles ("D: \ webroot ");
Foreach (VAR s in files)
{
Lstfi. Add (new system. Io. fileinfo (s ));
}
Gridview1.datasource = lstfi;
Gridview1.databind ();
}
Protected void packdown_click (Object sender, eventargs E)
{
Response. Clear ();
Response. contenttype = "application/zip ";
Response. addheader ("content-disposition", "filename=dotnetzip.zip ");
Using (zipfile zip = new zipfile (system. Text. encoding. Default) // solves Chinese garbled characters
{
Foreach (gridviewrow gvr in gridview1.rows)
{
If (checkbox) gvr. cells [0]. controls [1]). Checked)
{
Zip. AddFile ("D: \ webroot \" + (gvr. cells [1]. controls [1] As label). Text ,"");
}
}
Zip. Save (response. outputstream );
}
Response. End ();
}
}