在.net中下载多个文件作为zip [英] download multiple files as zip in .net

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

问题描述

我有一个文件列表,每个文件的复选框,如果用户检查许多文件,点击下载我必须压缩所有这些文件,并下载它...像邮件附件..

i have a file list with check box for each files , if the user checks many files and click download i have to zip all those files and download it... like in mails attachments..

我在这个中使用了代码提示发布单个文件下载

pls帮助如何下载多个文件为zip ..

pls help how to download multiple files as zip..

推荐答案

您需要打包文件并将结果写入响应。
您可以使用 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天全站免登陆