使用第三方组件: ICSharpCode.SharpZipLib
给按钮绑定一个点击事件
后台处理:
1 public ActionResult DownZip(string ids) 2 ????????{ 3 ????????????if (string.IsNullOrEmpty(ids)) 4 ????????????????return Content("请选择要操作的数据"); 5 ?6 ????????????var idArr = ids.Split(Constant.SEPARATOR.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); 7 ?8 ????????????#region 9 ????????????FileStream fs = null;10 ????????????byte[] buffer = null;11 ????????????string path = Server.MapPath("/Upload/Resource/压缩包.zip");12 ????????????13 ????????????using (ZipFile file = ZipFile.Create(path))14 ????????????{15 ????????????????file.BeginUpdate();16 17 ????????????????//通过这个名称格式化器,可以将里面的文件名进行一些处理。默认情况下,会自动根据文件的路径在zip中创建有关的文件夹。18 ????????????????file.NameTransform = new MyNameTransfom(); ???????????????19 20 ????????????????foreach (var id in idArr)21 ????????????????{22 ????????????????????var model = new BLL.APPResourceBLL().GetModel(Convert.ToInt32(id));23 ????????????????????file.Add(Server.MapPath(model.Path));24 ????????????????} ???????????????25 26 ????????????????file.CommitUpdate(); ??????????????27 ????????????}28 ????????????fs = new FileStream(path, FileMode.Open);29 ????????????buffer = new byte[fs.Length];30 ????????????fs.Position = 0;31 ????????????fs.Read(buffer, 0, buffer.Length);32 ????????????fs.Close();33 ????????????Response.Charset = "UTF-8";34 ????????????Response.Buffer = false;35 ????????????Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");36 ????????????Response.ContentType = "application/octet-stream";37 ????????????Response.AddHeader("Content-Disposition", "attachment; filename=压缩包.zip");38 ????????????Response.BinaryWrite(buffer);39 ????????????Response.Flush();40 ????????????Response.End();41 ????????????return new EmptyResult();42 ????????????#endregion ???????????43 ????????}
前台:
1 function downZipEvent() { 2 ????????if ($(".checkall:checked").size() < 1) { 3 ????????????$.dialog.alert(‘请选择下载文件!‘); 4 ????????????return false; 5 ????????} 6 ????????var ids = ‘‘; 7 ????????$.each($(".checkall:checked"), function () { 8 ????????????ids += $(this).attr(‘typeid‘) + ‘,‘; 9 ????????})10 11 ????????var form = $("<form>");12 ????????form.attr("style", "display:none");13 ????????form.attr("target", "");14 ????????form.attr("method", "post");15 ????????form.attr("action", "/AppManage/AppResource/DownZip");16 ????????var input1 = $("<input>");17 ????????input1.attr("type", "hidden");18 ????????input1.attr("name", "ids");19 ????????input1.attr("value", ids);20 ????????$("body").append(form);21 ????????form.append(input1);22 ????????form.submit();23 ????????form.remove();24 ????}
参考: http://blog.csdn.net/kongwei521/article/details/51167903
http://www.jb51.net/article/74867.htm
http://www.cnblogs.com/kissdodog/p/3525295.html
使用Jquery Ajax请求 下载压缩文件
原文地址:http://www.cnblogs.com/moy-1313133/p/7977818.html