asp.net 打包压缩文件并下载的全源码
asp.net打包压缩文件的界面文件可以打包下载的asp.net全部源码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MultipleFileDownLoadSample._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">
<div>
<asp:Button ID="btDownload" runat="server" Text="下载" onclick="btDownload_Click"
style="height: 26px" />
<hr />
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="文件名">
<ItemTemplate>
<asp:Label ID="file" runat="server" Text='<%# Eval("FileName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
asp.net打包压缩文件的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 ICSharpCode.SharpZipLib.Zip;
namespace MultipleFileDownLoadSample
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
DataBind();
}
}
public override void DataBind()
{
//读取images目录下面的文件列表,绑定到GridView上面
var query = from item in Directory.GetFiles(Server.MapPath("~/images"))
select new
{
FileName = Path.GetFileName(item)
};
gv.DataSource = query;
gv.DataBind();
}
private void Download(IEnumerable<string> files,string zipFileName ){
//根据所选文件打包下载
MemoryStream ms = new MemoryStream();
byte[] buffer = null;
using (ZipFile file = ZipFile.Create(ms))
{
file.BeginUpdate();
file.NameTransform = new MyNameTransfom();//通过这个名称格式化器,可以将里面的文件名进行一些处理。默认情况下,会自动根据文件的路径在zip中创建有关的文件夹。
foreach (var item in files)
{
file.Add(Server.MapPath(string.Format("~/images/{0}",item)));
}
try
{
file.CommitUpdate();
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
buffer = new byte[ms.Length];
ms.Position = 0;
ms.Read(buffer, 0, buffer.Length);
}
Response.AddHeader("content-disposition", "attachment;filename="+zipFileName);
Response.BinaryWrite(buffer);
Response.Flush();
Response.End();
}
protected void btDownload_Click(object sender, EventArgs e)
{
//var urls1 = "upload/zzzyk.com.txt;upload/yizuotu.net.jpg;";
//var urls = urls1.TrimEnd(';');
//根据复选框的选中状态,将多个文件打包下载
List<string> files = new List<string>();
//string[] urlstrs = urls.Split(';');
//foreach (var a in urlstrs)
//{
// files.Add(a);
//}
foreach (GridViewRow item in gv.Rows)
{
var chk = item.FindControl("chk") as CheckBox;
if (chk.Checked)
{
var label = item.FindControl("file") as Label;
files.Add(label.Text);
}
}
Download(files, "Test.zip");
}
}
public class MyNameTransfom : ICSharpCode.SharpZipLib.Core.INameTransform
{
#region INameTransform 成员
public string TransformDirectory(string name)
{
return null;
}
public string TransformFile(string name)
{
return Path.GetFileName(name);
}
#endregion
}
}