gpt4 book ai didi

c# - 在 .net 中将多个文件下载为 zip

转载 作者:可可西里 更新时间:2023-11-01 08:52:14 26 4
gpt4 key购买 nike

我有一个程序需要一次下载多个文件。我可以使用这个 single file download 下载单个文件,但它不适用于多个。

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

最佳答案

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

代码示例:

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

关于c# - 在 .net 中将多个文件下载为 zip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3984590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com