有时候需要将单个或多个文件进行压缩打包后在进行下载,这里我自定义了一个ActionResult,方便进行文件下载

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using WebSeat.Site.Member.Helper; namespace WebSeat.Site.Member.CustomResult
{
/// <summary>
/// 说明:压缩文件
/// 创建日期:2016/12/14 16:18:22
/// 创建人:曹永承
/// </summary>
public class ZipResult : ActionResult
{
#region 字段 private Ionic.Zip.ZipFile zip;
#endregion #region 属性
/// <summary>
/// 文档类型
/// </summary>
public string ContentType
{
get;set;
}
/// <summary>
/// 下载文件名称
/// </summary>
public string DownloadName { get; set; }
#endregion #region 构造函数
public ZipResult(string downLoadName =null)
{
ContentType = "application/x-zip-compressed";
DownloadName = downLoadName;
zip = new Ionic.Zip.ZipFile(System.Text.Encoding.UTF8);
} public ZipResult(params string[] filenames):this()
{
foreach (string filename in filenames)
{
zip.AddFile(filename);
}
} public ZipResult(IDictionary<string,Stream> dir, string downLoadName = " ")
: this(downLoadName)
{
foreach (string key in dir.Keys)
{
zip.AddEntry(key, dir[key]);
}
}
#endregion #region 公共方法
/// <summary>
/// 添加文件
/// </summary>
/// <param name="filename"></param>
public void AddFile(string filename)
{
zip.AddFile(filename);
} /// <summary>
/// 添加流
/// </summary>
/// <param name="entryName"></param>
/// <param name="stream"></param>
public void AddEntry(string entryName, Stream stream)
{
zip.AddEntry(entryName, stream);
} /// <summary>
/// 获取压缩后的流
/// </summary>
/// <returns></returns>
public Stream GetStream()
{
Stream stream = new MemoryStream();
zip.Save(stream);
return stream;
} #endregion #region 实现ExecuteResult方法
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.ContentType = ContentType;
string filename = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")+".zip";
filename = DownloadName == null ? filename : DownloadName; //对文件名称进行编码,避免下载文件名称出现乱码
filename = filename.EncodingDownloadFileName();
context.HttpContext.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Stream ms = GetStream();
context.HttpContext.Response.AddHeader("Content-Length", ms.Length.ToString());
ms.Seek(, SeekOrigin.Begin);
byte[] bytes = new byte[ * ];
int readSize = ;
var output = context.HttpContext.Response.OutputStream;
while ((readSize = ms.Read(bytes, , bytes.Length)) > )
{
output.Write(bytes, , readSize);
context.HttpContext.Response.Flush();
}
}
#endregion }
}

ASP.NET MVC自定义ActionResult实现文件压缩的更多相关文章

  1. ASP.NET MVC 自定义Razor视图WorkContext

    概述 1.在ASP.NET MVC项目开发的过程中,我们经常需要在cshtml的视图层输出一些公用信息 比如:页面Title.服务器日期时间.页面关键字.关键字描述.系统版本号.资源版本号等 2.普通 ...

  2. ASP.NET MVC之读取服务器文件资源的两种方式

    初次认识asp.net mvc时,以为所有文件都需要走一遍路由,然后才能在客户端显示, 所以我首先介绍这一种方式 比如说:我们在服务器上有图片: ~/resource/image/5.jpg 我们就需 ...

  3. asp.net mvc 自定义pager封装与优化

    asp.net mvc 自定义pager封装与优化 Intro 之前做了一个通用的分页组件,但是有些不足,从翻页事件和分页样式都融合在后台代码中,到翻页事件可以自定义,再到翻页和样式都和代码分离, 自 ...

  4. ASP.NET MVC 自定义路由中几个需要注意的小细节

    本文主要记录在ASP.NET MVC自定义路由时,一个需要注意的参数设置小细节. 举例来说,就是在访问 http://localhost/Home/About/arg1/arg2/arg3 这样的自定 ...

  5. Asp.net Mvc 自定义Session (二)

    在 Asp.net Mvc 自定义Session (一)中我们把数据缓存工具类写好了,今天在我们在这篇把 剩下的自定义Session写完 首先还请大家跟着我的思路一步步的来实现,既然我们要自定义Ses ...

  6. Asp.net mvc 自定义全局的错误事件HandleErrorAttribute无效

    Asp.net mvc 自定义全局的错误事件HandleErrorAttribute,结果无效, 原因: 1.没有在RegisterGlobalFilters 里面添加或者你要的位置添加. 2.你把这 ...

  7. ASP.NET MVC自定义验证Authorize Attribute(包含cookie helper)

    前几天Insus.NET有在数据库实现过对某一字段进行加密码与解密<使用EncryptByPassPhrase和DecryptByPassPhrase对MS SQLServer某一字段时行加密和 ...

  8. asp.net MVC 自定义模型绑定 从客户端中检测到有潜在危险的 Request.QueryString 值

    asp.net mvc 自定义模型绑定 有潜在的Requset.Form 自定义了一个模型绑定器.前端会传过来一些敏感字符.调用bindContext. valueProvider.GetValue( ...

  9. C# MVC 自定义ActionResult实现EXCEL下载

    前言 在WEB中,经常要使用到将数据转换成EXCEL,并进行下载.这里整理资料并封装了一个自定义ActionResult类,便于使用.如果文章对你有帮助,请点个赞. 话不多少,这里转换EXCEL使用的 ...

随机推荐

  1. 关于node.js杂记

    https://gitlore.com/page/gitlore-git/nodejs/index.html[node.js中文文档] //////    https://gitlore.com/in ...

  2. 微信小程序-页面链接

    navigator 页面链接. 注:navigator-hover默认为{background-color: rgba(0, 0, 0, 0.1); opacity: 0.7;}, <navig ...

  3. 安装composer

    按照composer官网的指导,运行下列命令:curl -sS https://getcomposer.org/installer | php长时间无反应.手动安装1.下载installer# wge ...

  4. matlab basic operation command

    Matlab basic operation: >> 5+6 ans = 11 >> 3*4 ans = 12 >> 2^6 ans = 64 >> 1 ...

  5. C# WebService服务Post提交

    public string WebServerTest(string PostData) { PostData = "jsonData=" + PostData; string P ...

  6. 运行Shell脚本的几种方式解析

    1 给脚本加上执行权限chmod u+x a.sh, 而后就可以直接用全路径来执行脚本了,比如当前文件夹下用./a.sh, 如果脚本所在目录在PATH环境变量之中, 则直接用a.sh即可 2 sh/b ...

  7. MySQL存储IP地址操作

    数据库数据表创建语法: DROP TABLE IF EXISTS `admin`; CREATE TABLE IF NOT EXISTS `admin`( `adminid` INT UNSIGNED ...

  8. git入门网站

    http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 git入门教程:对商业的.开源的. ...

  9. KEGG数据库

    参考:KEGG数据库中文教程 - 博奥  &[学习笔记]KEGG数据库 - 微信 学习一个技能最主要的事情你必须知道,那就是能通过它来做什么? KEGG数据库里面有什么? 如何查询某一特定的代 ...

  10. [分享] 《步步为营封 Win7》--skyfree

    [分享] <步步为营封 Win7>--skyfree Skyfree 发表于 2009-9-13 05:51:32 https://www.itsk.com/thread-20957-1- ...