Moin Svenkan,
ich habe hier mal ein, stark zusammengestrichenes, Beispiel für den Download per TidHTTP.
Das müsste jetzt noch auf den
FTP-Upload angepasst werden, ausserdem habe ich hier auch nur ein Beispiel drin.
Delphi-Quellcode:
uses
Classes,IdHTTP,IdComponent;
type
TcsDownloadThread = class(TThread)
private
FsURL : string;
FsFilepath : string;
FHTTP : TidHTTP;
FWork : TWorkEvent;
FWorkSender : TObject;
FWorkMode : TWorkMode;
FWorkCount : integer;
function GetWorkEvent: TWorkEvent;
procedure SetWorkEvent(const Value: TWorkEvent);
{$IFDEF VER180}
procedure DoWork(ASender: TIdBaseObject; AWorkMode: TWorkMode;AWorkCount: Integer);
{$ELSE}
procedure DoWork(Sender: TObject; AWorkMode: TWorkMode;const AWorkCount: Integer);
{$ENDIF}
procedure SyncWork;
public
constructor Create(const AsURL : string;const AsFilepath : string);
destructor Destroy; override;
procedure Execute; override;
property OnWork : TWorkEvent read GetWorkEvent write SetWorkEvent;
end;
implementation
uses SysUtils,windows,csfileutils,csspecial;
constructor TcsDownloadThread.Create(const AsURL, AsFilepath: string);
begin
inherited Create(true);
FreeOnTerminate := false;
FsURL := AsURL;
FsFilepath := AsFilepath;
FWork := nil;
FHTTP := TIdHTTP.Create(nil);
FHTTP.OnWork := DoWork;
end;
destructor TcsDownloadThread.Destroy;
begin
if FHTTP.Connected then FHTTP.Disconnect;
FreeAndNil(FHTTP);
inherited;
end;
{$IFDEF VER180}
procedure TcsDownloadThread.DoWork(ASender: TIdBaseObject; AWorkMode: TWorkMode;AWorkCount: Integer);
begin
FWorkSender := Sender;
FWorkMode := AWorkMode;
FWorkCount := AWorkCount;
Synchronize(SyncWork);
end;
{$ELSE}
procedure TcsDownloadThread.DoWork(Sender: TObject; AWorkMode: TWorkMode; const AWorkCount: Integer);
begin
FWorkSender := Sender;
FWorkMode := AWorkMode;
FWorkCount := AWorkCount;
Synchronize(SyncWork);
end;
{$ENDIF}
procedure TcsDownloadThread.Execute;
var
ms : TMemoryStream;
dwStart : DWORD;
begin
try
try
FHTTP.Head(FsURL);
except
exit;
end;
ms := TMemoryStream.Create;
try
try
FHTTP.Get(FsURL,ms);
except
exit;
end;
ms.SaveToFile(FsFilepath);
finally
FreeAndNil(ms);
end;
finally
if FHTTP.Connected then FHTTP.Disconnect;
dwStart := GetTickCount;
while FHTTP.Connected and ((GetTickCount-dwStart) < 10000) do;
Terminate;
end;
end;
function TcsDownloadThread.GetWorkEvent: TWorkEvent;
begin
Result := FWork;
end;
procedure TcsDownloadThread.SetWorkEvent(const Value: TWorkEvent);
begin
FWork := Value;
end;
procedure TcsDownloadThread.SyncWork;
begin
if Assigned(FWork) then FWork(FWorkSender,FWorkMode,FWorkCount);
end;