Autor Beitrag
Janoschka
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 23



BeitragVerfasst: Di 09.07.02 15:20 
Ich benutze diese funktion um eine Datei über's http protokoll zu saugen - funtzt wunderprächtig!
ausblenden volle Höhe Quelltext
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;

ich kann damit jedoch keine dateien parallel saugen - hat jemand ne Idee, oder source wie ich mehrere Dateien gleichzeitig downloaden kann?
Tino
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Veteran
Beiträge: 9839
Erhaltene Danke: 45

Windows 8.1
Delphi XE4
BeitragVerfasst: Di 09.07.02 15:40 
Du könntest die Funktion jeweils in einem eingenen Thread laufen lassen. Schau mal in die Delphi-Hilfe unter Thread nach. Dort findest Du dann schon mal die Grundlegenden Infos zu diesem Thema!

Gruß