如何通过TIdHTTP下载大文件?
我使用此代码下载小文件:
I use this code to download small files:
Var
ms:TMemoryStream;
begin
ms:=TMemoryStream.Create;
Idhttp1.get('http://mydomain.com/myfile.zip',ms);
ms.SaveToFile('myfile.zip');
ms.Free;
end;
但是文件在存储到磁盘之前已保存在RAM中,因此可能难以下载> 1Gb的文件, 例如。有没有一种按部分下载文件的方法?还是我需要使用WinInet?
But file is saved in RAM before storing to disk, so it may be difficult to download files >1Gb, for example. Is there a way to download a file by its parts? Or do I need to use the WinInet? Thanks in advance!
TMemoryStream
提供了一个内存缓冲区,因此,如果您将其下载为一个,则需要有足够的内存来容纳收到的所有内容。不过,这不是唯一的流。您可以根据需要传递 Get
方法任何类型的流,包括将接收到的内容写到磁盘的流。例如,使用 TFileStream
。
TMemoryStream
provides an in-memory buffer, so if you download into one, you need to have enough memory to hold everything you receive. It's not the only kind of stream, though. You can pass the Get
method any kind of stream you want, including one that writes its contents to disk as it receives it. Use TFileStream
, for example.
var
s: TStream;
s := TFileStream.Create('myfile.zip', fmCreate);
try
IdHttp1.Get(..., s);
finally
s.Free;
end;
在任何地方调用 LoadFromFile
或 TMemoryStream
上的$ c> SaveToFile , TFileStream
可能是一个更好的选择。
Anywhere you call LoadFromFile
or SaveToFile
on a TMemoryStream
, it's possible that TFileStream
is a better choice.