压缩文件相关的类:

    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 + "&param" + i + "=" + selectedList[i].toString());
}
location.href = url;
});

asp.net mvc 文件压缩下载的更多相关文章

  1. ASP.NET MVC 文件上传和路径处理

    ASP.NET MVC 文件上传和路径处理总结 目录 文件的上传和路径处理必须解决下面列出的实际问题: 1.重复文件处理 2.单独文件上传 3.编辑器中文件上传 4.处理文章中的图片路径 5.处理上传 ...

  2. ASP.NET MVC文件上传【转】

    最近用到了文件上传功能,下面给出ASP.NET MVC文件上传的一个简单示例: 一.前端代码 @using (Html.BeginForm("UploadFile", " ...

  3. 实现asp.net的文件压缩、解压、下载

    很早前就想做文件的解压.压缩.下载 了,不过一直没时间,现在项目做完了,今天弄了下.不过解压,压缩的方法还是看的网上的,嘻嘻~~不过我把它们综合了一下哦.呵呵~~ 1.先要从网上下载一个icsharp ...

  4. asp.net mvc 上传下载文件的几种方式

    view: <!DOCTYPE html> <html> <head> <meta name="viewport" content=&qu ...

  5. php多文件压缩下载

    /*php多文件压缩并且下载*/ function addFileToZip($path,$zip){ $handler=opendir($path); //打开当前文件夹由$path指定. whil ...

  6. Java 多个文件压缩下载

    有时候会有多个附件一起下载的需求,这个时候最好就是打包下载了 首先下面这段代码是正常的单个下载 public void Download(@RequestParam("file_path&q ...

  7. mvc 文件压缩 减少文件大小

    using System; using System.Collections.Generic; using System.IO.Compression; using System.Linq; usin ...

  8. ASP.NET Core文件压缩最佳实践

    前言 在微软官方文档中,未明确指出文件压缩功能的使用误区. 本文将对 ASP.NET Core 文件响应压缩的常见使用误区做出说明. 误区1:未使用 Brotil 压缩 几乎不需要任何额外的代价,Br ...

  9. asp.net mvc + javascript生成下载文件

    近期做的是对现有项目进行重构.WEB FROM改成MVC,其实也算是推倒重来了. 里面有一个导出功能,将数据输出成txt文件,供下载.原先的做法是有一个隐藏的iframe,在这个iframe的页面中设 ...

随机推荐

  1. 关于ckeditor过滤掉html样式标签之我见

    1.CKEDITOR编辑器属性可以通过修改/ckeditor/config.js文件来控制 //标签过滤默认是开启的,默认会过了<style>样式标签设置为true可关闭过滤config. ...

  2. masonry使用问题

    2015年11月3日 coreData的学习练习中复习使用masonry自动布局 masonry自动布局发现问题: 两个控件的相对布局: 如果被参考对象用这个带anchor的属性,就会报这样一个错误: ...

  3. nginx libpcre.so.1: cannot open shared object file

    linux 64位安装nginx后启动出错报以下错误 1 2 3 [root@localhost nginx-1.3.0]# /usr/local/nginx/sbin/nginx error whi ...

  4. MySQL整理碎片

    1 innodb引擎表 alter table TABLE_NAME engine='innodb'; 还有一种方法 optiize table TABLE_NAME; http://stackove ...

  5. iOS NSMutableArray添加NSInteger元素

    NSMutableArray *array = [[NSMutableArray alloc] init]; NSInteger num = 7; NSNumber *number = [NSNumb ...

  6. PHP 实现定时任务的几种方法

    一. 简单直接不顾后果型 <?php ignore_user_abort();//关掉浏览器,PHP脚本也可以继续执行. set_time_limit(0);// 通过set_time_limi ...

  7. 深入理解linux网络技术-P179

    上锁 net_device结构的组织一节可知,dev_base列表以及dev_name_head和dev_name_index两张hash表由dev_base_list锁保护.然而,该锁只用于对列表和 ...

  8. 自定义开关ToggleButton

    package com.example.test;import android.os.Bundle;import android.app.Activity;import android.view.Me ...

  9. Java传参

    1.  如果参数是基本数据类型(int.long等),传值.方法内部改变参数值,外部值不变. 2.  如果参数是对象类型,传地址.方法内部改变对象值,外部对象值改变.但是,如果方法内部调用new重新构 ...

  10. 灵感闪现 篇 (一) 2d场景 3d 效果

    中途打断一下 ,框架文档的 更新. 另开一篇主题为 灵感闪现的 板块. 在工作生活中,总有发现新事物或新东西 而让自己突然 灵感闪现的时候,那么这个时候,我必须要抓住,并尽快把 这份灵感实现下来. 之 ...