Batch download of multiple ASP. NET files)

Source: Internet
Author: User

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 ();

}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.