如何下载存储在数据库中的多个文件作为字节

时间:2016-03-27 06:50:36

标签: c# angularjs asp.net-mvc-4

我以字节的形式将文件存储在数据库中。我们要求将所有附件下载为zip文件。请建议

1 个答案:

答案 0 :(得分:2)

试试这个

protected void ZipDownload()
    {
        var list = //query for getting the files.
        ZipFile zip = new ZipFile();
        foreach (var file in list)
        {

            zip.AddEntry(file.docname, (byte[])file.doc.ToArray());
        }
        var zipMs = new MemoryStream();
        zip.Save(zipMs);
        byte[] fileData = zipMs.GetBuffer();
        zipMs.Seek(0, SeekOrigin.Begin);
        zipMs.Flush();
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=docs.zip ");
        Response.ContentType = "application/zip";
        Response.BinaryWrite(fileData);
        Response.End();

    }