前端代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<!--添加外部jQuery脚本-->
<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>
<!--普通固定url下载-->
<a href="http://localhost:56657/api/Zip">本地测试下载</a>
<br />
<textarea id="fileUrl" placeholder="填写多个文件路径换行符分隔"></textarea>
<br />
<input id="getname" type="text" placeholder="填写生成的压缩名文件字" value="myzip.zip" />
<!--根据表单参数生成url下载-->
<a id="download" href="#">本地测试下载2</a>
<br />
</body>
</html>
<script>
$(function () {
$("#download").click(function () {
//根路径
var baseUrl = "http://localhost:56657/api/Zip";
var fileUrls = $("#fileUrl").val().split(/[(\r\n)\r\n]+/);
var last = fileUrls.join(',');
var getname = $("#getname").val();
//拼接参数url
baseUrl = baseUrl + "?ids=" + last + "&getname=" + getname;
//打开新窗口下载
window.open(baseUrl);
});
})
</script>
后端代码
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.IO;
using System.Web;
using System.Web.Http;
namespace ZipTest.Controllers
{
public class ZipController : ApiController
{
public void Get()
{
string ids = HttpContext.Current.Request.QueryString["ids"];
string getname= HttpContext.Current.Request.QueryString["getname"];
ZipParam body = new ZipParam()
{
files = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries),// new string[] { "D:\\1.txt", "D:\\2\\2.txt" },//
zipFileName = getname//"myzip.zip" //"mytxt.txt"
};
#region 原始
//MemoryStream ms = new MemoryStream();
//byte[] buffer = null;
//using (ZipFile file = ZipFile.Create(ms))
//{
// file.BeginUpdate();
// //file.NameTransform = new MyNameTransfom();
// foreach (var item in body.files)
// {
// if (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();
//}
//HttpContext.Current.Response.Clear();
//HttpContext.Current.Response.Buffer = true;
//HttpContext.Current.Response.ContentType = "application/x-zip-compressed";
//HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(body.zipFileName));
//HttpContext.Current.Response.BinaryWrite(buffer);
//HttpContext.Current.Response.Flush();
//HttpContext.Current.Response.End();
#endregion
#region 新
//基础输出流
MemoryStream memoryStream = new MemoryStream();
//使用压缩流
using (ZipOutputStream zipOutputStream = new ZipOutputStream(memoryStream)) //HttpContext.Current.Response.OutputStream
{
foreach (var item in body.files)
{
//文件流
FileStream fileStream = File.Open(item, FileMode.Open);
string fileName = fileStream.Name;
ZipEntry zipEntry = new ZipEntry(fileName);
zipEntry.DateTime = DateTime.Now;
zipOutputStream.PutNextEntry(zipEntry);
fileStream.CopyTo(zipOutputStream);
fileStream.Close();
zipOutputStream.Flush();
}
zipOutputStream.Finish();
zipOutputStream.Close();
}
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/octet-stream";// "text/plain";// "application /x-zip-compressed";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(body.zipFileName));
HttpContext.Current.Response.BinaryWrite(memoryStream.ToArray());//
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
#endregion
}
}
public class ZipParam
{
public string[] files { get; set; }
public string zipFileName { get; set; }
}
}
其他参见:
header中Content-Disposition的作用 https://mp.youkuaiyun.com/postedit/90411252
MIME http content-type https://mp.youkuaiyun.com/postedit/90409200
C# 获取文件名及扩展名 https://mp.youkuaiyun.com/postedit/90298912