uses wininet;
function DownloadFile(
url,filename:
string) : Boolean ;
var
Stream : TFileStream ;
NetHandle : HINTERNET ;
UrlHandle : HINTERNET;
BytesRead : DWORD ;
// DWORD, not cardinal
Buffer :
array[0..1024]
of Char ;
begin
Result := false ;
NetHandle := InternetOpen('
Delphi download function',
INTERNET_OPEN_TYPE_DIRECT,
Nil,
Nil,0);
if Assigned(NetHandle)
then
begin
UrlHandle := InternetOpenUrl(NetHandle, PChar(
url),
nil, 0,
INTERNET_FLAG_RELOAD, 0);
if Assigned(UrlHandle)
then
begin
FillChar(Buffer, SizeOf(Buffer), 0);
Stream := TFileStream.Create(filename,fmCreate);
try
while InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead)
and (BytesRead > 0)
do
Stream.WriteBuffer(Buffer, BytesRead);
Result := TRUE;
finally
Stream.Free;
end;
end;
// if Assigned(UrlHandle)
InternetCloseHandle(UrlHandle);
end;
InternetCloseHandle(NetHandle);
end;