ASP.NET 将所有文件下载为 Zip

发布于 2024-08-29 23:34:25 字数 175 浏览 6 评论 0原文

我的网络服务器上有一个文件夹,其中有数百个 mp3 文件。我想为用户提供从网页下载目录中每个 mp3 的压缩存档的选项。

我想仅在需要时以编程方式压缩文件。由于 zip 文件非常大,出于性能原因,我认为需要在压缩时将 zip 文件发送到响应流

这可能吗?我该怎么做呢?

I have a folder on my web server that has hundreds of mp3 files in it. I would like to provide the option for a user to download a zipped archive of every mp3 in the directory from a web page.

I want to compress the files programmatically only when needed. Because the zip file will be quite large, I am thinking that I will need to send the zip file to the response stream as it is being zipped, for performance reasons.

Is this possible? How can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

此岸叶落 2024-09-05 23:34:25

这是我用 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);
}

Here is code I use to do this with DotNetZip - works very well. Obviously you will need to provide the variables for outputFileName, folderName, and 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);
}
久光 2024-09-05 23:34:25

我不敢相信这有多么容易。阅读此内容后,这是我使用的代码:

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();
}

I can't believe how easy this was. After reading this, here is the code that I used:

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();
}
岁月无声 2024-09-05 23:34:25

您可以添加一个自定义处理程序(.ashx 文件),该处理程序获取文件路径、读取文件并使用压缩库对其进行压缩,然后将具有正确内容类型的字节返回给最终用户。

You could add a custom handler (.ashx file) that takes the file path, reads the file compresses it using a compression library and returns the bytes to the end user with the correct content-type.

话少情深 2024-09-05 23:34:25
            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();
            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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文