《工作日记》2019-01-29 文件下载
Posted zlkkkkkk
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《工作日记》2019-01-29 文件下载相关的知识,希望对你有一定的参考价值。
Controller层
public void DownLoadFiles(string ids) { try { string str = Convert.ToString(ids); if (ids != null && !string.IsNullOrWhiteSpace(str)) { List<string> strPath = new List<string>(); string[] idlist = str.Substring(0, str.Length - 1).Split(‘,‘); foreach (string id in idlist) { var list = StudengService.LoadEntites(o => o.Stu_ID == Convert.ToInt32(id)); if (list != null && list.Count() > 0) { foreach (var model in list) { var detail = detailService.LoadEntites(o => o.ID == Convert.ToInt32(id)).First(); if (detail != null) { string strAddress = model.Data_Address; strPath.Add(Server.MapPath(@"ContentFiles") + strAddress); } } } } ZipFileDownload(strPath.ToArray(), "资料文件.zip"); } } catch (Exception ex) { } }
/// <summary>
/// 批量进行多个文件压缩到一个文件
/// </summary>
/// <param name="files">文件列表(绝对路径)</param> 这里用的数组,你可以用list 等或者
/// <param name="zipFileName">生成的zip文件名称</param>
private void ZipFileDownload(string[] files, string zipFileName)
{
MemoryStream ms = new MemoryStream();
byte[] buffer = null;
/// 批量进行多个文件压缩到一个文件
/// </summary>
/// <param name="files">文件列表(绝对路径)</param> 这里用的数组,你可以用list 等或者
/// <param name="zipFileName">生成的zip文件名称</param>
private void ZipFileDownload(string[] files, string zipFileName)
{
MemoryStream ms = new MemoryStream();
byte[] buffer = null;
using (ZipFile file = ZipFile.Create(ms))
{
file.BeginUpdate();
file.NameTransform = new MyNameTransfom();
foreach (var item in files)
{
if (System.IO.File.Exists(item))
{
file.Add(item);
}
}
file.CommitUpdate();
buffer = new byte[ms.Length];
ms.Position = 0;
ms.Read(buffer, 0, buffer.Length); //读取文件内容(1次读ms.Length/1024M)
ms.Flush();
ms.Close();
}
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("content-disposition", "attachment;filename=" + zipFileName);
Response.BinaryWrite(buffer);
Response.Flush();
Response.End();
}
{
file.BeginUpdate();
file.NameTransform = new MyNameTransfom();
foreach (var item in files)
{
if (System.IO.File.Exists(item))
{
file.Add(item);
}
}
file.CommitUpdate();
buffer = new byte[ms.Length];
ms.Position = 0;
ms.Read(buffer, 0, buffer.Length); //读取文件内容(1次读ms.Length/1024M)
ms.Flush();
ms.Close();
}
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("content-disposition", "attachment;filename=" + zipFileName);
Response.BinaryWrite(buffer);
Response.Flush();
Response.End();
}
View层
<span class="btn"><a href="#" class="btn-download">下载</a></span> var jsonData = ""; $(".checkchild:checked").each(function () { jsonData += $(this).val() + ","; }); if (jsonData.length > 0) { window.location.href = "/Data/DownLoadFiles?ids=" + jsonData; }else{ $.alert({ title: "Message", content: "请勾选要下载的数据", }); }
以上是关于《工作日记》2019-01-29 文件下载的主要内容,如果未能解决你的问题,请参考以下文章