博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp.net 批量下载实现(打包压缩下载)
阅读量:6293 次
发布时间:2019-06-22

本文共 5674 字,大约阅读时间需要 18 分钟。

1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MultipleFileDownLoadSample._Default" %>        

View Code

2.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 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
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/file/82cbf858-92d9-4a82-95f7-7c9ba75004c7.jpg;upload/file/0df0dd6c-7d10-43ac-b583-e0a67eea9ba0.jpg;upload/file/d83d903f-4850-4607-a65d-4ac1a96d3791.jpg;upload/file/f0b23b86-4461-4ebd-8afe-eced6784a8dc.jpg;upload/file/c4062bd1-56cd-4801-b5f1-aa29f7b0cd44.jpg;upload/file/eb5bd59d-adee-4ac0-8ae7-706a9d8df0fc.pdf;upload/file/46ef5099-9480-4c47-9a09-8e585a7d97ff.txt;upload/file/7a20cd9e-c0a4-4fd6-9998-40d4d6d835fb.txt;upload/file/37842f30-aa10-4433-a641-55010c8a0efc.doc;upload/file/e8575946-9f09-4aed-b9dd-a4f0d3f617e0.png;upload/file/745f8304-8175-4e6c-9200-f5c3c95dcbd8.doc;upload/file/9372ba27-c05b-4873-91a0-3d3e5ffff9b6.xls;upload/file/160f5052-86e7-400e-aa13-7427af409f3d.txt;upload/file/8fcd56ee-e06b-46b4-b100-52f4349688b1.txt;upload/file/f11e433e-fad7-4b61-8a0e-a5bd7650ce4c.txt;upload/file/4410e83d-cc6f-4d24-91af-a117979f6bc0.jpg;upload/file/3bf0b545-38c0-4e24-a8a6-25dc5ea97eea.jpg;upload/file/22e3ea67-fcad-4d58-b39e-048661608ae8.jpg;upload/file/cc6162ca-8640-4f26-ad39-71ec1b287eae.txt;upload/file/cfcd3c53-ff84-4a54-bf3c-85dd5b3e8e6d.pdf;upload/file/668404f7-e808-4d3a-8818-d01fbba1f42f.pdf;upload/file/59f8d59d-4d3b-4262-83ab-bd18d5f345e3.pdf;upload/file/9da50650-daff-40b3-b9c9-4067b3f565bd.jpg;upload/file/1a3150a3-eecb-48c1-aabb-e5d4f6847ca4.jpg;upload/file/dfe943ed-6432-424e-a881-70442dc833de.jpg;upload/file/4a2005a1-f68f-4289-979a-b7d936383a7c.jpg;upload/file/9a208b9e-2591-45bd-800a-1d67ef974cce.jpg;upload/file/f00b9046-7f4a-4154-af71-662ff7b5ec3e.jpg;upload/file/57c03a84-02bc-43f9-8f8a-d158ad67c9cf.doc;upload/file/3761e5a4-4238-4234-8ea5-c3d90987ae3c.xls;upload/file/5c8a60f4-ac9e-4b65-9f26-3d7073ff9762.txt;upload/file/add4390c-ed11-40b4-a599-9553a308ee02.txt;upload/file/cb790b7b-2c50-48f6-b82c-eda06c6b2fc4.txt;upload/file/c3ccfef9-4a76-4767-9ad3-e197c2667d0c.txt;upload/file/048066be-081e-4bf8-be24-abca2ee560c0.xls;upload/file/506f7f11-dd40-4838-b1cf-af4382f6b8d6.jpg;upload/file/4201a8b1-96f1-4312-9736-0eb682263b90.jpg;upload/file/be2963b7-66bc-4f5f-bb42-886ec08c11ab.txt;upload/file/4f455ab5-42f8-4a01-b21c-1627aff73706.jpg;"; //var urls = urls1.TrimEnd(';'); //根据复选框的选中状态,将多个文件打包下载 List
files = new List
(); //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 }}
View Code

 

转载于:https://www.cnblogs.com/bobo-show/p/4730379.html

你可能感兴趣的文章
再次分享一个多选文件上传方案“.NET研究”
查看>>
PySide教程:一个简单的点击“.NET研究”按钮示例
查看>>
find命令
查看>>
网络通讯程序整理(一)
查看>>
[转载]一站式WPF--Window
查看>>
poj-1159 Palindrome **
查看>>
VS2010/VS 2013 删除空行
查看>>
解决linux ssh登陆缓慢问题
查看>>
将二叉查找树转化为链表的代码实现
查看>>
[转]宽字符的介绍
查看>>
UIScrollView用法
查看>>
SQL 判断两个时间段是否有交叉
查看>>
python打包_cx_freeze
查看>>
web.config/app.config敏感数据加/解密的二种方法
查看>>
PHP监控linux服务器负载
查看>>
delphi 入门教程
查看>>
技术人员,你拿什么拯救你的生活----温水煮青蛙
查看>>
最小排列数
查看>>
[C] zintrin.h: 智能引入intrinsic函数 V1.01版。改进对Mac OS X的支持,增加INTRIN_WORDSIZE宏...
查看>>
uva 101 The Blocks Problem
查看>>