function TForm1.DownloadFile(
const URL:
String;
const FileName:
String)
var
KbpsCounter: Integer = 0;
Kbps: Integer;
Ticks, BytesRead: DWORD;
Buffer:
Array [0..1023]
of Byte;
DownloadFile:
File;
hSession, hRequest: HINTERNET;
begin
Result := False;
(* Session öffnen *)
hSession := InternetOpen('
AppTitel', INTERNET_OPEN_TYPE_DIRECT,
nil,
nil, 0);
if hSession <>
nil then
begin
(* URL öffnen *)
hRequest := InternetOpenUrl(hSession,
URL,
nil, 0, INTERNET_FLAG_RELOAD, 0);
if hRequest <>
nil then
begin
(* Download starten
vllt checken ob Datei schon existiert? *)
AssignFile(DownloadFile, FileName);
try
Rewrite(DownloadFile, 1);
Ticks := GetTickCount;
while True
do
begin
(* Daten auslesen *)
if InternetReadFile(hRequest, @Buffer, 1024, BytesRead) = False
then
Break;
(* kbps jede Sekunde berechnen *)
Inc(KbpsCounter, BytesRead);
if Ticks <= (GetTickCount - 1000)
then
begin
Kbps := KbpsCounter / 1024;
// Label1.Caption := IntToStr(Kbps) + ' Kilobyte pro Sekunde';
KbpsCounter := 0;
Ticks := GetTickCount;
end;
(* In Datei schreiben *)
if BytesRead <> 0
then
BlockWrite(DownloadFile, Buffer, BytesRead);
else //Download beendet
begin
Result := True;
Break;
end;
Application.ProcessMessages;
end;
finally
CloseFile(DownloadFile);
CloseInternetHandle(hRequest);
end;
end;
CloseInternetHandle(hSession);
end;
end;