在 .net 中以 zip 格式下载多个文件 [英] download multiple files as zip in .net

查看:20
本文介绍了在 .net 中以 zip 格式下载多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序需要一次下载多个文件.我可以使用此单个文件下载来下载单个文件,但它不适用于多个.

I have a program which needs to download multiple files at once. I can download a single file by using this single file download, but it doesn't work for multiple.

如何一次下载多个文件,例如 zip 文件?

How can one download multiple files at once in such as as zip file?

推荐答案

您需要打包文件并将结果写入响应.您可以使用 SharpZipLib 压缩库.

You need to pack files and write a result to a response. You can use SharpZipLib compression library.

代码示例:

Response.AddHeader("Content-Disposition", "attachment; filename=" + compressedFileName + ".zip");
Response.ContentType = "application/zip";

using (var zipStream = new ZipOutputStream(Response.OutputStream))
{
    foreach (string filePath in filePaths)
    {
        byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);

        var fileEntry = new ZipEntry(Path.GetFileName(filePath))
        {
            Size = fileBytes.Length
        };

        zipStream.PutNextEntry(fileEntry);
        zipStream.Write(fileBytes, 0, fileBytes.Length);
    }

    zipStream.Flush();
    zipStream.Close();
}

这篇关于在 .net 中以 zip 格式下载多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆