我有一个文件夹放在我的网络服务器上,里面有数百个mp3文件。我希望在网页上提供一个选项,让用户下载该目录下所有mp3的压缩归档文件。
我想要在需要时通过编程方式压缩文件。考虑到zip文件会非常大,为了性能原因,我认为我需要在压缩时将zip文件发送到响应流中。
这种做法可行吗?我该如何实现?
我想要在需要时通过编程方式压缩文件。考虑到zip文件会非常大,为了性能原因,我认为我需要在压缩时将zip文件发送到响应流中。
这种做法可行吗?我该如何实现?
这是我使用DotNetZip完成此操作的代码 - 运行非常好。显然,您需要为outputFileName、folderName和includeSubFolders提供变量。
response.ContentType = "application/zip";
response.AddHeader("content-disposition", "attachment; filename=" + outputFileName);
using (ZipFile zipfile = new ZipFile()) {
zipfile.AddSelectedFiles("*.*", folderName, includeSubFolders);
zipfile.Save(response.OutputStream);
}
zipfile.AddSelectedFiles("*.*", folderName, direcotryInZip, includeSubFolders);
。 - marquito我简直不敢相信这有多么容易。在阅读这个之后,这是我使用的代码:
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.BufferOutput = false;
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=pauls_chapel_audio.zip");
using (ZipFile zip = new ZipFile())
{
zip.CompressionLevel = CompressionLevel.None;
zip.AddSelectedFiles("*.mp3", Server.MapPath("~/content/audio/"), "", false);
zip.Save(Response.OutputStream);
}
Response.Close();
}
foreach (GridViewRow gvrow in grdUSPS.Rows)
{
CheckBox chk = (CheckBox)gvrow.FindControl("chkSelect");
if (chk.Checked)
{
string fileName = gvrow.Cells[1].Text;
string filePath = Server.MapPathfilename);
zip.AddFile(filePath, "files");
}
}
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=DownloadedFile.zip");
Response.ContentType = "application/zip";
zip.Save(Response.OutputStream);
Response.End();