1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60:
| uses WinInet;
function DownloadInetFile (const fileURL, FileName: String; pgb : TProgressbar): boolean; const BufferSize = 1024; var hSession, hURL : HInternet; allright : Boolean; Buffer : array[1..BufferSize] of Byte; BufferLen : DWORD; fs : TFileStream; code : array[1..20] of char; codeLen, index : cardinal; size : String; begin Result := False; hUrl := nil; hSession := nil; fs := nil; try If not FileExists(fileName) then fs := TFileStream.Create(FileName, fmCreate or fmShareExclusive) else fs := TFileStream.Create(FileName, fmOpenReadWrite or fmShareExclusive); hSession := InternetOpen(PChar(Filename), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0); If hSession = nil then exit; hURL := InternetOpenURL(hSession, PChar(fileURL), nil, 0, 0, 0); If hUrl = nil then exit else begin codeLen:= 10; index:= 0; HttpQueryInfo(hURL, HTTP_QUERY_CONTENT_LENGTH, @code,codeLen,index); size:= pchar(@code); pgb.Max:= StrToInt(size); end; pgb.Position:= 0; repeat application.processmessages; allright := InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen); fs.WriteBuffer(Buffer, BufferLen); pgb.Position:= pgb.Position + BufferLen; pgb.Update; until (BufferLen = 0) or not allright; If not allright then begin raise Exception.Create('Fehler beim herunterladen der Datei.'); exit; end; Result := True; finally If Assigned(hUrl) then InternetCloseHandle(hURL); If Assigned(hSession) then InternetCloseHandle(hSession); If Assigned(fs) then FreeAndNil(fs); end; end; |