以zip格式下载多个文件 [英] Download Multiple files as zip
本文介绍了以zip格式下载多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在asp.net中以ZIP格式下载多个文件
代码如下
i am trying to download multiple files as ZIP in asp.net
the code is following
var downloadFileName = string.Format(projectno, DateTime.Now.ToString("yyyy-MM-dd-HH_mm_ss"));
Response.ContentType = "application/zip";
Response.AddHeader("Content-Disposition", "filename=" + downloadFileName );
using (var zip = new ZipFile())
{
var readMeMessage = string.Format("Your ZIP file {0} contains the following files:{1}{1}", downloadFileName, Environment.NewLine);
foreach (GridViewRow gvr in grdUploads.Rows)
if (((CheckBox)gvr.Cells[6].FindControl("dwnldCheck")).Checked == true)
{
string filename = Server.MapPath("../" + ((Label)gvr.Cells[3].FindControl("lblTitle")).Text);
readMeMessage += string.Concat("\t* ", filename, Environment.NewLine);
zip.AddFile(filename, projectno);
}
zip.AddEntry("README.txt", readMeMessage, Encoding.ASCII);
zip.Save(Response.OutputStream);
}
一切正常,但保存为未知格式,而不是rar文件
请帮助
everything is working fine,but its saving as unknown format,not as rar file
please help
推荐答案
您需要在第三行的downloadFileName之后在代码中添加扩展名.
You need to add extension in your code after downloadFileName at 3rd line.
var downloadFileName = string.Format(projectno, DateTime.Now.ToString("yyyy-MM-dd-HH_mm_ss"));
Response.ContentType = "application/zip";
Response.AddHeader("Content-Disposition", "filename=" + downloadFileName + ".zip");
using (var zip = new ZipFile())
{
var readMeMessage = string.Format("Your ZIP file {0} contains the following files:{1}{1}", downloadFileName, Environment.NewLine);
foreach (GridViewRow gvr in grdUploads.Rows)
if (((CheckBox)gvr.Cells[6].FindControl("dwnldCheck")).Checked == true)
{
string filename = Server.MapPath("../" + ((Label)gvr.Cells[3].FindControl("lblTitle")).Text);
readMeMessage += string.Concat("\t* ", filename, Environment.NewLine);
zip.AddFile(filename, projectno);
}
zip.AddEntry("README.txt", readMeMessage, Encoding.ASCII);
zip.Save(Response.OutputStream);
}
您可以按照以下说明进行操作
You can do it as mentioned below
<pre lang="c#">ZipFile multipleFilesAsZipFile = new ZipFile();
Response.AddHeader("Content-Disposition", "attachment; filename=DownloadedZipFile.zip");
Response.ContentType = "application/zip";
foreach (ListItem fileName in checkBoxList.Items)
{
if (fileName.Selected)
{
string filePath = Server.MapPath("~/www.CsharpAspNetArticles.com/" + fileName.Value);
multipleFilesAsZipFile.AddFile(filePath,string.Empty);
}
}
multipleFilesAsZipFile.Save(Response.OutputStream);
请参考将多个文件下载为Asp.Net中的Zip文件有关更多信息
Refer Download Multiple Files As Zip File In Asp.Net for more info
我看不到将扩展名.zip
添加到downloadFileName的位置.我注意到的一件事是,您使用了很多var关键字,而不是声明强类型变量.就我而言,如果downloadFileName是字符串,则将其声明为此类,这是不正确的做法,应该避免使用.
I fail to see where you are adding the extension .zip
to the downloadFileName . One thing I noticed is that you use a lot of var keyword, instead of declaring strong type variables. As far as I am concerned his is bad practice, and should really be avoided, if downloadFileName is a string declare it as such.
这篇关于以zip格式下载多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文