刚好这次项目中遇到了这个东西,就来弄一下,挺简单的,但是前台调用的时候弄错了,浪费了大半天的时间,本人也是菜鸟一枚。开始吧。(MVC的)
@using Rattan.Core.Utility;
@{
string ButtonScript = string.Empty;
}
@if (Rattan.Basic.Globals.GetIsAuth(ViewBag.AuthValues, "QuickExport"))
{
ButtonScript = @"
{
position: 0,
id: 'btnQuickExport',
text: '导出稿件附件',
disabled: false,
icon: 'pwd.gif',
handler: function ()
{
QuickExport();
}
}";
}
<script type="text/javascript" language="javascript">
helper.onBeforePageLoad = function (params) {
}
helper.onAfterPageLoad = function () {
m_grid.insertToolBar(
[
@Html.Raw(ButtonScript)
]);
}
</script>
<script type="text/javascript" language="javascript">
function QuickExport() {
// var rows = m_grid.getSelections(); ---这个是不打包,一个个跳出来的,没卵用,当时应付的
// $.each(rows, function (i, row) {
// var url = row.filelist + "?" + Math.random();
// window.setTimeout(function () {
// window.open(url, "_blank");
// }, 1000);
// });
var rows = m_grid.getSelections();
var Draft_Id_Array = [];
$.each(rows, function (i, row) {
Draft_Id_Array.push(row.Draft_Id);
});
if (!Draft_Id_Array.length) {
alert("至少选中一行!");
return;
}
var url = "/BasicInfo/Meeting_Draft/ZipFileByCode";
var postParams = {
Draft_Ids: Draft_Id_Array.join(',')
};
window.open(url + '?Draft_Ids=' + Draft_Id_Array.join(','))
}
</script>
/// <summary>
/// 压缩打包文件
/// </summary>
public ActionResult ZipFileByCode(string Draft_Ids)//string Draft_Id
{
MemoryStream ms = new MemoryStream();
byte[] buffer = null;
using (ZipFile file = ZipFile.Create(ms)) //压缩包创建到内存
{
file.BeginUpdate();
file.NameTransform = new IndexController();//通过这个名称格式化器,可以将里面的文件名进行一些处理。默认情况下,会自动根据文件的路径在zip中创建有关的文件夹。
string where = "Draft_Id in (" + Draft_Ids + ")";
var draftInfo = Meeting_DraftRepository.Instance.GetMany(where).ToList();
foreach (var draftInfoList in draftInfo)
{
if (string.IsNullOrWhiteSpace(draftInfoList.filelist) || draftInfoList.filelist.IndexOf('/') < 0)
{
continue;
}
var positonIndex = draftInfoList.filelist.LastIndexOf('/');
string fileName = draftInfoList.filelist.Substring(positonIndex, draftInfoList.filelist.Length - positonIndex);//客户端保存的文件名
file.Add(Server.MapPath(draftInfoList.filelist));
}
file.CommitUpdate();
buffer = new byte[ms.Length];
ms.Position = 0;
ms.Read(buffer, 0, buffer.Length);
}
//将中文转换预防出现乱码
string excelName = "稿件附件".ToString();
excelName = HttpUtility.UrlEncode(excelName, System.Text.Encoding.UTF8);
DateTime dt = DateTime.Now;
Response.AddHeader("content-disposition", "attachment;filename=" + excelName + DateTime.Now.ToString("yyyyMMdd")+ ".zip");
Response.BinaryWrite(buffer);
Response.Flush();
Response.End();
return View();
}
}