asp.net mvc 文件压缩下载
压缩文件相关的类:
public class ZIPCompressUtil
{
public static Tuple<bool, Stream> Zip(string strZipTopDirectoryPath, int intZipLevel, string strPassword, string[] filesOrDirectoriesPaths)
{
try
{
List<string> AllFilesPath = new List<string>();
if (filesOrDirectoriesPaths.Length > ) // get all files path
{
for (int i = ; i < filesOrDirectoriesPaths.Length; i++)
{
if (File.Exists(filesOrDirectoriesPaths[i]))
{
AllFilesPath.Add(filesOrDirectoriesPaths[i]);
}
else if (Directory.Exists(filesOrDirectoriesPaths[i]))
{
GetDirectoryFiles(filesOrDirectoriesPaths[i], AllFilesPath);
}
}
} if (AllFilesPath.Count > )
{
MemoryStream ms = new MemoryStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(ms);
zipOutputStream.SetLevel(intZipLevel);
zipOutputStream.Password = strPassword; for (int i = ; i < AllFilesPath.Count; i++)
{
string strFile = AllFilesPath[i];
try
{
if (Directory.Exists(strFile)) //folder
{
string strFileName = strFile.Replace(strZipTopDirectoryPath, "");
if (strFileName.StartsWith(""))
{
strFileName = strFileName.Substring();
}
ZipEntry entry = new ZipEntry(strFileName + "/");
entry.DateTime = DateTime.Now;
zipOutputStream.PutNextEntry(entry);
}
else //file
{
FileStream fs = File.OpenRead(strFile); byte[] buffer = new byte[fs.Length];
fs.Read(buffer, , buffer.Length); string strFileName = strFile.Replace(strZipTopDirectoryPath, "");
if (strFileName.StartsWith(""))
{
strFileName = strFileName.Substring();
}
ZipEntry entry = new ZipEntry(strFileName);
entry.DateTime = DateTime.Now;
zipOutputStream.PutNextEntry(entry);
zipOutputStream.Write(buffer, , buffer.Length); fs.Close();
fs.Dispose();
}
}
catch
{
continue;
}
} zipOutputStream.Finish();
return new Tuple<bool, Stream>(true, ms);
}
else
{
return new Tuple<bool, Stream>(false, null);
}
}
catch
{
return new Tuple<bool, Stream>(false, null);
}
} private static void GetDirectoryFiles(string strParentDirectoryPath, List<string> AllFilesPath)
{
string[] files = Directory.GetFiles(strParentDirectoryPath);
for (int i = ; i < files.Length; i++)
{
AllFilesPath.Add(files[i]);
}
string[] directorys = Directory.GetDirectories(strParentDirectoryPath);
for (int i = ; i < directorys.Length; i++)
{
GetDirectoryFiles(directorys[i], AllFilesPath);
}
if (files.Length == && directorys.Length == ) //empty folder
{
AllFilesPath.Add(strParentDirectoryPath);
}
}
}
调用并提供下载的方法
public ActionResult Export()
{
List<long> productIds = null;
var url = Request.RawUrl;
if (url.Contains("?"))
{
string paramStr = url.Substring(url.LastIndexOf("?"));
string[] paramArray = paramStr.Split('&');
productIds = paramArray
.Select(item => item.Split('=').Length > ? item.Split('=')[] : "")
.Select(value => value.TryLong()).ToList();
} if (Directory.Exists(Server.MapPath("~/上架商品")))
{
Directory.Delete(Server.MapPath("~/上架商品"), true);
} var productlist = ProductOnSaleService.Instance.GetProductSkuImgsExportList(productIds);
foreach (var productItem in productlist)
{
var dir = Server.MapPath("~/上架商品/") + productItem.ProductId + "-" + productItem.LongName;
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
foreach (var skuItem in productItem.Skus)
{
Bitmap rqBtimap = QrCodeHelper.Encode(BizKeyValService.Instance.Get(EnumBizKey.ProductShareUrl).BizVal
+ "?id=" + productItem.ProductId + "&skuid=" + skuItem.SkuId, , );
rqBtimap.Save(dir + "/" + productItem.ProductId + "-" + skuItem.SkuId + "-" + skuItem.SkuName + ".jpg"); }
} var strZipTopDirectoryPath = Server.MapPath("~/");
const int intZipLevel = ;
const string strPassword = "";
var filesOrDirectoriesPaths = new string[] { Server.MapPath("~/上架商品") };
var result = ZIPCompressUtil.Zip(strZipTopDirectoryPath, intZipLevel, strPassword, filesOrDirectoriesPaths);
var buffer = new byte[result.Item2.Length];
result.Item2.Position = ;
result.Item2.Read(buffer, , buffer.Length);
result.Item2.Close();
Response.AppendHeader("content-disposition", "attachment;filename=上架商品.zip");
Response.BinaryWrite(buffer);
Response.Flush();
Response.End();
return new EmptyResult();
}
js代码
$("#btnExpress").click(function () {
var selectedList = $.getSelectId(true);
if (selectedList.length == 0) {
$.alert("请选择商品");
return;
}
var url = "/Product/ProductOnSale/Export";
for (var i = 0; i < selectedList.length; i++) {
url = (i == 0 ? url + "?param" + i + "=" + selectedList[i].toString()
: url + "¶m" + i + "=" + selectedList[i].toString());
}
location.href = url;
});
asp.net mvc 文件压缩下载的更多相关文章
- ASP.NET MVC 文件上传和路径处理
ASP.NET MVC 文件上传和路径处理总结 目录 文件的上传和路径处理必须解决下面列出的实际问题: 1.重复文件处理 2.单独文件上传 3.编辑器中文件上传 4.处理文章中的图片路径 5.处理上传 ...
- ASP.NET MVC文件上传【转】
最近用到了文件上传功能,下面给出ASP.NET MVC文件上传的一个简单示例: 一.前端代码 @using (Html.BeginForm("UploadFile", " ...
- 实现asp.net的文件压缩、解压、下载
很早前就想做文件的解压.压缩.下载 了,不过一直没时间,现在项目做完了,今天弄了下.不过解压,压缩的方法还是看的网上的,嘻嘻~~不过我把它们综合了一下哦.呵呵~~ 1.先要从网上下载一个icsharp ...
- asp.net mvc 上传下载文件的几种方式
view: <!DOCTYPE html> <html> <head> <meta name="viewport" content=&qu ...
- php多文件压缩下载
/*php多文件压缩并且下载*/ function addFileToZip($path,$zip){ $handler=opendir($path); //打开当前文件夹由$path指定. whil ...
- Java 多个文件压缩下载
有时候会有多个附件一起下载的需求,这个时候最好就是打包下载了 首先下面这段代码是正常的单个下载 public void Download(@RequestParam("file_path&q ...
- mvc 文件压缩 减少文件大小
using System; using System.Collections.Generic; using System.IO.Compression; using System.Linq; usin ...
- ASP.NET Core文件压缩最佳实践
前言 在微软官方文档中,未明确指出文件压缩功能的使用误区. 本文将对 ASP.NET Core 文件响应压缩的常见使用误区做出说明. 误区1:未使用 Brotil 压缩 几乎不需要任何额外的代价,Br ...
- asp.net mvc + javascript生成下载文件
近期做的是对现有项目进行重构.WEB FROM改成MVC,其实也算是推倒重来了. 里面有一个导出功能,将数据输出成txt文件,供下载.原先的做法是有一个隐藏的iframe,在这个iframe的页面中设 ...
随机推荐
- Q_DISABLE_COPY
QObject 中没有提供一个拷贝构造函数和赋值操作符给外界使用,其实拷贝构造和赋值的操作都是已经声明了的,但是它们被使用了Q_DISABLE_COPY() 宏放在了private区域.因此所有继承自 ...
- iOS 开发之照片框架详解之二 —— PhotoKit 详解(上)
转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-two.html 一. 概况 本文接着 iOS 开 ...
- Oracle Sql优化之Merge 改写优化Update
1.待改写语句如下 update table1 f )),) ,),)), f.jine2 )),) from table2 e where e.kjqj=f.kjqj=e.gs=f.gs and e ...
- 转:jmeter实践
本文主要介绍性能测试中的常用工具jmeter的使用方式,以方便开发人员在自测过程中就能自己动手对系统进行自动压测和模拟用户操作访问请求.最后还用linux下的压测工具ab做了简单对比. 1. ...
- 使用DTM ( Dynamic Topic Models )进行主题演化实验
最近想研究下Dynamic Topic Models(DTM),论文看了看,文科生的水平确实是看不懂,那就实验一下吧,正好Blei的主页上也提供了相应的C++工具, http://www.cs.pri ...
- My workbench draft
System Linux / Ubuntu 14.04.5 LTS (Trusty Tahr) + ROS Indigo Linux / Ubuntu 16.04.1 LTS (Xenial Xeru ...
- ViewPager滑动标签-PagerSlidingTabStrip的使用
有篇博客写的已经非常详细,所以不再写了.主要在于导入这个Library,导入Library看自己的笔记 博客地址:http://doc.okbase.net/HarryWeasley/archive/ ...
- Json解析要点
解析Json时,昨天遇到了新的问题,之前都是解析的数组,不是数组的用类来做. 这是Json串; {"status":"00001","ver" ...
- hdu_5213_Lucky(莫队算法+容斥定理)
题目连接:hdu_5213_Lucky 题意:给你n个数,一个K,m个询问,每个询问有l1,r1,l2,r2两个区间,让你选取两个数x,y,x,y的位置为xi,yi,满足l1<=xi<=r ...
- PHP使用Redis
首先确保安装了Redis扩展 详细的使用方式 请浏览扩展文档 https://github.com/phpredis/phpredis#connection $obj = new Redis(); $ ...