概述
var fs = new MemoryStream();
var zipStream = new ZipOutputStream(fs);
//创建一个条目
foreach (var file in files)
{
var stream = new FileStream(file, FileMode.Open);
var name = Path.GetFileName(file);
var zipEntry = new ZipEntry(name);
zipStream.PutNextEntry(zipEntry);
stream.CopyTo(zipStream);
zipStream.CloseEntry();
stream.Flush();
stream.Close();
}
zipStream.Finish();
fs.Position = 0;
var rep = new HttpResponseMessage(HttpStatusCode.OK);
rep.Content = new StreamContent(fs);
rep.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
var disPosition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
var userAgent = Request.Headers.UserAgent.ToString().ToUpper();
//IE浏览器文件名乱码处理
string filename = $"virtual.zip";
if (userAgent.Contains("MSIE") || userAgent.Contains("TRIDENT") || userAgent.Contains("EDGE"))
{
disPosition.FileName = HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8);
disPosition.Name = HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8);
disPosition.Size = (long)filename.Length;
}
else
{
disPosition.FileName = filename;
disPosition.Name = filename;
disPosition.Size = (long)filename.Length;
}
rep.Content.Headers.ContentDisposition = disPosition;
return rep;
最后
以上就是精明皮卡丘为你收集整理的C# 打包zip文件 并通过webapi返回的全部内容,希望文章能够帮你解决C# 打包zip文件 并通过webapi返回所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复