使用 DotNetZip 通过 ASP.NET MVC 下载 zip 文件 [英] Downloading of zip file through ASP.NET MVC using DotNetZip
本文介绍了使用 DotNetZip 通过 ASP.NET MVC 下载 zip 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在一个文件夹中创建了一个文本文件并压缩了该文件夹并保存了@same 位置以供测试.我想在创建后直接在用户计算机上下载该 zip 文件.我正在使用 dotnetzip 库并完成了以下操作:
I have created a text file in a folder and zipped that folder and saved @same location for test purpose. I wanted to download that zip file directly on user machine after it is created. I am using dotnetzip library and have done following:
Response.Clear();
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "filename=" + "sample.zip");
using (ZipFile zip = new ZipFile())
{
zip.AddDirectory(Server.MapPath("~/Directories/hello"));
zip.Save(Server.MapPath("~/Directories/hello/sample.zip"));
}
有人可以建议如何在用户端下载 zip 文件吗?
Can someone please suggest how the zip file can be downloaded at user's end.?
推荐答案
你可以使用控制器的 File
方法返回一个文件,比如:
You may use the controller's File
method to return a file, like:
public ActionResult Download()
{
using (ZipFile zip = new ZipFile())
{
zip.AddDirectory(Server.MapPath("~/Directories/hello"));
zip.Save(Server.MapPath("~/Directories/hello/sample.zip"));
return File(Server.MapPath("~/Directories/hello/sample.zip"),
"application/zip", "sample.zip");
}
}
如果不需要另外存储zip文件,则不必将其写入服务器上的文件中:
If the zip file is not required otherwise to be stored, it is unnecessary to write it into a file on the server:
public ActionResult Download()
{
using (ZipFile zip = new ZipFile())
{
zip.AddDirectory(Server.MapPath("~/Directories/hello"));
MemoryStream output = new MemoryStream();
zip.Save(output);
return File(output.ToArray(), "application/zip", "sample.zip");
}
}
这篇关于使用 DotNetZip 通过 ASP.NET MVC 下载 zip 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文