尝试使用 TIdHTTP 下载文件时出现“扩展内存流时内存不足”错误

问题描述

我正在尝试使用 Delphi 10.4 中的标准 TIdHTTPTIdSSLIOHandler 组件从网络服务器下载文件

memorystream := TMemoryStream.Create;
http.request.useragent:='Mozilla/5.0 (Windows NT 5.1; rv:2.0b8) Gecko/20100101 Firefox/4.0b8';
http.HandleRedirects := True;
try
  http.get('https://dl.nmkd.de/ai/clip/coco/coco.ckpt',memorystream);
except
  on E:Exception do memo.lines.add(E.Message);
  memo.lines.add(http.responsetext);
end;
application.ProcessMessages;
memorystream.SavetoFile('coco.ckpt');
memorystream.free;

我在备忘录中得到的回复是:

Out of memory while expanding memory stream
HTTP/1.1 200 OK

文件大小约为 9 GB。

如何让 MemoryStream 能够处理该大小?我在 PC 上安装了足够多的物理 RAM。

解决方法

不是将资源临时加载到内存中,而是直接使用 TFileStream 将其加载到本地文件:

Stream := TFileStream.Create('coco.cpkt');
try
  try
    http.get('https://dl.nmkd.de/ai/clip/coco/coco.ckpt',Stream);
  except
    on E:Exception do memo.lines.add(E.Message);
  end;
finally
  Stream.Free;
end;