function Download(
const Url, FileName:
string;
var Progress: Single): Boolean;
const
BUFFER_SIZE = 1024 * 5;
var
hOpen, hUrl: HINTERNET;
buff:
array[0..BUFFER_SIZE - 1]
of Char;
buffLen: DWORD;
currentBytes, totalBytes: DWORD;
f:
File;
begin
result := false;
buffLen := SizeOf(buff);
ZeroMemory(@buff, buffLen);
currentBytes := 0;
totalBytes := 0;
hOpen := InternetOpen(PChar('
Test'), INTERNET_OPEN_TYPE_PRECONFIG,
nil,
nil, 0) ;
try
hUrl := InternetOpenUrl(hOpen, PChar(
Url),
nil, 0, INTERNET_FLAG_DONT_CACHE, 0) ;
try
HttpQueryInfo(hUrl, HTTP_QUERY_CONTENT_LENGTH, @buff, buffLen, totalBytes);
totalBytes := StrToInt(
string(buff));
AssignFile(f, FileName);
Rewrite(f, 1);
repeat
InternetReadFile(hUrl, @buff, SizeOf(buff), buffLen);
currentBytes := currentBytes + buffLen;
Progress := currentBytes / totalBytes;
BlockWrite(f, buff, buffLen);
until buffLen = 0;
CloseFile(f) ;
result := true;
finally
InternetCloseHandle(hUrl);
end;
finally
InternetCloseHandle(hOpen);
end;
end;