Asp.net MVC 生成zip并下载
前面有生成Excel或Word的示例,所以就不再重新写了。
这里只提供将指定文件以ZIP的方式下载。
创建一个 Zip工具类
public class ZIPCompressUtil
{
public static byte[] Zip(List<string> AllFilesPath)
{
try
{
if (AllFilesPath.Count > )
{
MemoryStream ms = new MemoryStream();
byte[] buffer = null; using (ZipFile file = ZipFile.Create(ms))
{
file.BeginUpdate();
file.NameTransform = new MyNameTransfom();
//通过这个名称格式化器,可以将里面的文件名进行一些处理。默认情况下,会自动根据文件的路径在zip中创建有关的文件夹。 for (int i = ; i < AllFilesPath.Count; i++)
{
string strFile = AllFilesPath[i];
file.Add(strFile);
} file.CommitUpdate(); buffer = new byte[ms.Length];
ms.Position = ;
ms.Read(buffer, , buffer.Length);
}
return buffer; }
return null;
}
catch
{
return null;
} }
}
在ActionResulit中调用Zip工具类。
public ActionResult ExportZIP()
{
List<string> fileAll = new List<string>();
//将所有文件所在的地址添加到fileAll中
...操作代码 //调用ZIP工具类,将集合传递给它
var buffer = ZIPCompressUtil.Zip(fileAll); Response.AppendHeader("content-disposition", "attachment;filename=名称.zip");
Response.BinaryWrite(buffer);
Response.Flush();
Response.End();
}
如果大家有什么好的想法,可以留言,我肯定会学习并实践好再拿出来分享。
非常感谢。
如果对您有帮助,请点赞!
Asp.net MVC 生成zip并下载的更多相关文章
- ASP.NET MVC应用程序实现下载功能
ASP.NET MVC应用程序实现下载功能 上次Insus.NET有在MVC应用程序实现了上传文件的功能<MVC应用程序显示上传的图片> http://www.cnblogs.com/in ...
- 多个文件下载打包生成zip格式下载
这个多个文件下载生成zip格式必须先引用一个ICSharpCode.SharpZipLib.dll. 代码如下 //将多个文件打包成压缩文件zip格式下载 protected voi ...
- ASP.Net MVC 生成安全验证码
---------html <td>验证码:</td> <td> <img src="/Logi ...
- ASP.NET MVC生成安全验证码
html部分: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- (一)【转】asp.net mvc生成验证码
网站添加验证码,主要为防止机器人程序批量注册,或对特定的注册用户用特定程序暴力破解方式,以进行不断的登录.灌水等危害网站的操作.验证码被广泛应用在注册.登录.留言等提交信息到服务器端处理的页面中. ...
- ASP.NET网页生成EXCEL并下载(利用DataGrid或GridView等)
前几天要在后台查询数据库内容(用entity framework),将查询出来的信息(List或DataTable形式)转成EXCEL供用户下载.经过谷歌.百度搜索,终于搜出了一些代码.似乎可用了,结 ...
- Asp.net mvc生成验证码
1.生成验证码类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- Asp.net MVC 填充word并下载
使用Aspose.word填充内容并下载(免费版Aspose) 填充固定模式的Word文档,需要先制作Word模板: Aspose.Words主要通过Words里域(Fields)来控制内容. ...
- ASP.NET MVC 生成验证码
using System.Web.Mvc; using System.Drawing; using System; using System.Drawing.Imaging; using Models ...
随机推荐
- css定位讲解
定位分三种:相对定位,绝对定位,固定定位; 相对定位:position:relatve; 绝对定位:position:absolute; 固定定位:position:fixed; 一, ...
- Jxl创建Excel文件和解析Excel文件
import java.io.File; import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableSheet; im ...
- Entity Framework——常见报错总结
1 实体属性配置为IsRequired()对更新的影响 抛出异常类型DbEntityValidationException 表结构: 实体: public class User { public in ...
- linux下expect命令实现批量ssh免密
有时候我们需要批量发送ssh命令给服务器,但是有可能有些服务器是新加入的,还没有配置ssh免密,这个时候就会提示我们输入yes/no 或者password等,expect脚本命令就是用于在提示这些的时 ...
- 问题(一)---线程池,锁、堆栈和Hashmap相关
一.线程池: 多线程技术主要解决处理器单元内多个线程执行的问题,它可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力. 假设一个服务器完成一项任务所需时间为:T1 创建线程时间,T2 在线程中 ...
- 异常:Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException
这个异常是出现在注入配置文件中配置好的属性时报错的: Injection of autowired dependencies failed; nested exception is java.lang ...
- 基于 HTML5 Canvas 的简易 2D 3D 编辑器
不管在任何领域,只要能让非程序员能通过拖拽来实现 2D 和 3D 的设计图就是很牛的,今天我们不需要 3dMaxs 等设计软件,直接用 HT 就能自己写出一个 2D 3D 编辑器,实现这个功能我觉得成 ...
- 使用Bitbucket Pipeline进行.Net Core项目的自动构建、测试和部署
1. 引言 首先,Bitbucket提供支持Mercurial和Git版本控制系统的网络托管服务.简单来说,它类似于GitHub,不同之处在于它支持个人免费创建私有项目仓库.除此之外,Bitbucke ...
- Nginx负载均衡使用ip
upstream test1{ server 192.168.1.213; server 192.168.1.37; } server { listen 80; # default backlog=2 ...
- SpringMvc+JavaConfig+Idea 搭建项目
1.介绍 之前搭建SpringMvc项目要配置一系列的配置文件,比如web.xml,applicationContext.xml,dispatcher.xml.Spring 3.X之后推出了基于Jav ...