• ASP.NET多文件批量打包下载


    在项目实施中,遇到了将多个文件一起打包后,提供给用户下载。如:在一个ASP.NET的开发项目中,通过一个GridView选中对应行数据的CheckBox,就可以实现对选中文件的打包下载了。

    在对多文件打包中用到了 DotNetZip 的方法来实现对多文件压缩打包。需要到http://dotnetzip.codeplex.com/处下载该文件,然后引用即可。

    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>图片打包下载</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <p>
    <asp:Button ID="PackDown" runat="server" Text="打包下载" OnClick="PackDown_Click" /></p>
    <asp:GridView ID="GridView1" runat="server" Width="500px"
    CellPadding="8" CellSpacing="1">
    <Columns>
    <asp:TemplateField HeaderText="<input type="checkbox"/>" InsertVisible="False">
    <ItemTemplate>
    <asp:CheckBox ID="CheckBox1" runat="server" />
    </ItemTemplate>
    <ItemStyle HorizontalAlign="Center" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="文件列表" 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))//解决中文乱码问题
    {
    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, "");//AddFile()第二个参数填写时不打包路径
    }
    }
    
    
    zip.Save(Response.OutputStream);//输出到浏览器下载
    }
    
    
    Response.End();
    }
    }

      

  • 相关阅读:
    加解密工具类(含keystore导出pfx)
    Java使用数字证书加密通信(加解密/加签验签)
    关于javax.crypto.BadPaddingException: Blocktype错误的几种解决方法
    产生定长的随机数
    数字证书生成--加密密/加签验签
    随机指定范围内N个不重复的数
    sql2012还原sql2008备份文件语句
    uploadify API
    海量数据处理分析
    .net经验积累
  • 原文地址:https://www.cnblogs.com/softwaredeveloper/p/4554491.html
Copyright © 2020-2023  润新知