【问题标题】:System.outofmemory exception while downloading large size zip下载大尺寸 zip 时出现 System.outofmemory 异常
【发布时间】:2019-11-14 13:52:09
【问题描述】:

我正在下载一个包含多个图像的大尺寸 zip 文件。它工作正常,直到 200mb,但在这之后它给了我 System.outofmemory 异常。我想下载一个大约(700 - 800mb)的大尺寸 zip。有什么办法可以克服 System.outofmemory 异常并下载大尺寸的 zip 文件?

【问题讨论】:

  • 请显示您正在使用的代码 - 如果没有它,我们将无法为您提供帮助。另外,请说明是服务器提供崩溃的 zip 文件,还是客户端。
  • 我正在使用以下代码,但在 zip.save().var outputStream = new MemoryStream(); 附近出现 Outofmemoryexception列出 文件 = objOrder.GetFileName(photoIds).ToList();使用 (var zip = new ZipFile()) { zip.ParallelDeflateThreshold = -1; zip.AddDirectoryByName(zipName.Trim()); for (int i = 0; i

标签: c# asp.net model-view-controller


【解决方案1】:

我不确定您是如何下载 zip 文件的,但请使用以下内容:

using (WebClient client = new WebClient())
{
    // fileUri = URI of the file you are downloading
    // filename = Target filepath to write the file
    client.DownloadFile(fileUri, filename);
}

使用WebClient.DownloadFile() 将导致文件被下载并分段保存在磁盘上,而不是整个内存对象。这将解决内存不足的问题。

WebClient 位于System.Net

如果您随后想要解压缩该文件,您可以针对您的磁盘文件而不是MemoryStreambyte 数组运行该操作。

【讨论】:

  • 我也使用 WebClient.DownloadFile() 但它工作正常。我在 zip.save() 附近遇到异常。
  • @AjayRathore 所以这个错误与下载文件无关吗?和解压的时候有关系吗?
  • 是的,当我下载 zip 文件或创建 zip 文件时出现错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-21
  • 2016-09-08
  • 1970-01-01
  • 1970-01-01
  • 2012-07-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多