我这里就也多个文件压缩为单个压缩文件下载为列
1.提供一个类
public class FileModel
{
public string FilePath { get; set; }
public string FileName { get; set; }
}
2.提供一个压缩的帮助类
public class ZipHelper
{
//压缩文件 p 为客户端传回来的文件列表:文件名+压缩包的名称
public static void ZipFile(Dictionary<string, string> dicFile, string zipName, out string zipPath)
{
zipPath = string.Empty;
//压缩文件的路径
string serverPath = ConfigurationManager.AppSettings["ZipPath"];
if (zipName != "") //压缩包名称不为空
{
zipPath = serverPath + zipName;
ZipOutputStream u = new ZipOutputStream(File.Create(zipPath));//新建压缩文件流 “ZipOutputStream”
foreach (var item in dicFile)
{
AddZipEntry(item.Key, item.Value, u, out u); //向压缩文件流加入内容
}
u.Finish(); // 结束压缩
u.Close();
}
}
//添加压缩项目:p 为需压缩的文件或文件夹; u 为现有的源ZipOutputStream; out j为已添加“ZipEntry”的“ZipOutputStream”
private static void AddZipEntry(string fileName, string filePath, ZipOutputStream u, out ZipOutputStream j)
{
if (File.Exists(filePath)) //文件的处理
{
u.SetLevel(9); //压缩等级
FileStream f = File.OpenRead(filePath);
byte[] b = new byte[f.Length];
f.Read(b, 0, b.Length); //将文件流加入缓冲字节中
ZipEntry z = new ZipEntry(fileName);
u.PutNextEntry(z); //为压缩文件流提供一个容器
u.Write(b, 0, b.Length); //写入字节
f.Close();
}
j = u; //返回已添加数据的“ZipOutputStream”
}
}
3.提供一个接口,并且返回文件流
/// <summary>
/// 压缩多个文件
/// </summary>
/// <param name="files">需要压缩的多个文件信息</param>
/// <param name="zpiFileName">生成的压缩文件名称</param>
public FileContentResult DownloadBulk(string filesJson,string zpiFileName)
{
List<FileModel> files= JsonHelper.JsonToObject<List<FileModel>>(filesJson);
Dictionary<string, string> dicPaths = new Dictionary<string, string>();
foreach (FileModel item in files)
{
dicPaths.Add(item.FileName, item.FilePath);
}
string zipPath = string.Empty;
string zipName = zpiFileName + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".zip";
HashSet<string> fileList = new HashSet<string>();
ZipHelper.ZipFile(dicPaths, zipName, out zipPath);
fileList.Add(zipPath);
//文件路径
string filePath = Path.Combine(Directory.GetCurrentDirectory(), zipPath);
byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, zipName);
}
4.请求接口,设置请求的方式
// 获取XMLHttpRequest
let xmlResquest = new XMLHttpRequest();
// 发起请求
xmlResquest.open("POST", '@Url.Action("DownloadBulk", "DownLoad")', true);
// 设置请求头类型
xmlResquest.setRequestHeader("Content-type", "application/json");
xmlResquest.responseType = "blob";
// 返回
xmlResquest.onload = function (oEvent) {
let content = xmlResquest.response;
//调用方法下载文件流
$scope.download(content, zipFileName + '.zip');
};
xmlResquest.send(JSON.stringify({
filesJson: JSON.stringify(fils_list),
zpiFileName: zipFileName
}));
5.提供一个下载文件流的方法
// 下载 content:文件流,DisplayName:文件名称
$scope.download=function(content, DisplayName) {
// 处理返回的文件流
//主要是将返回的data数据通过blob保存成文件
var blob = new Blob([content], {
type: "application/pdf;chartset=UTF-8"
});
if ('download' in document.createElement('a')) {
// 非IE下载
var elink = document.createElement('a');
elink.download = DisplayName;
elink.style.display = 'none';
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
URL.revokeObjectURL(elink.href); // 释放URL 对象
document.body.removeChild(elink);
} else {
// IE10+下载
navigator.msSaveBlob(blob, DisplayName);
}
}