繁体   English   中英

尝试使用Ionic.Zip dll将多个文件下载为Zip文件时出现OutOfMemory异常

[英]OutOfMemory exception when trying to download multiple files as a Zip file using Ionic.Zip dll

这是我用来使用Ionic.Zip dll下载多个文件作为zip文件的工作代码。 文件内容存储在SQL数据库中。 如果我尝试一次下载1-2个文件,则此程序有效,但是如果我尝试下载多个文件,则抛出OutOfMemory异常,因为某些文件可能很大。

尝试写入outputStream时发生异常。

如何改进此代码以下载多个文件,或者有更好的方法来逐个下载多个文件,而不是将它们压缩为一个大文件?

码:

public ActionResult DownloadMultipleFiles()
{
    string connectionString = "MY DB CONNECTIOBN STRING";

    List<Document> documents = new List<Document>();
    var query = "MY LIST OF FILES - FILE METADA DATA LIKE FILEID, FILENAME";
    documents = query.Query<Document>(connectionString1).ToList();

    List<Document> DOCS = documents.GetRange(0, 50); // 50 FILES

    Response.Clear();     
    var outputStream = new MemoryStream();

    using (var zip = new ZipFile())
    {
        foreach (var doc in DOCS)
        {
            Stream stream = new MemoryStream();
            byte[] content = GetFileContent(doc.FileContentId); // This method returns file content
            stream.Write(content, 0, content.Length);

            zip.UseZip64WhenSaving = Zip64Option.AsNecessary // edited

            zip.AddEntry(doc.FileName, content);
        }

        zip.Save(outputStream);
    }

    return File(outputStream, "application/zip", "allFiles.zip");
}

将文件下载到光盘而不是内存中,然后使用Ionic从光盘压缩文件。 这意味着您不需要一次将所有文件都存储在内存中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM