📄 ftpcli.pas
字号:
property ShareMode : TFtpShareMode read GetShareMode
write SetShareMode;
property DisplayFileMode : TFtpDisplayFileMode read GetDisplayFileMode
write SetDisplayFileMode;
property ConnectionType : TFtpConnectionType read GetConnectionType
write SetConnectionType;
property ProxyServer : string read FProxyServer
write FProxyServer;
property SocksPassword : string read GetSocksPassword
write SetSocksPassword;
property SocksPort : string read GetSocksPort
write SetSocksPort;
property SocksServer : string read GetSocksServer
write SetSocksServer;
property SocksUserCode : string read GetSocksUserCode
write SetSocksUserCode;
property OnDisplay : TFtpDisplay read FOnDisplay
write FOnDisplay;
property OnDisplayFile : TFtpDisplay read FOnDisplayFile
write FOnDisplayFile;
property OnError : TFTPDisplay read FOnError
write FOnError;
property OnCommand : TFtpCommand read FOnCommand
write FOnCommand;
property OnResponse : TNotifyEvent read FOnResponse
write FOnResponse;
property OnProgress : TFtpProgress read FOnProgress
write FOnProgress;
property OnSessionConnected : TSessionConnected read FOnSessionConnected
write FOnSessionConnected;
property OnSessionClosed : TSessionClosed read FOnSessionClosed
write FOnSessionClosed;
property OnRequestDone : TFtpRequestDone read FOnRequestDone
write FOnRequestDone;
property OnStateChange : TNotifyEvent read FOnStateChange
write FOnStateChange;
property OnReadyToTransmit : TFtpReadyToTransmit read FOnReadyToTransmit
write FOnReadyToTransmit;
property OnBgException : TBgExceptionEvent read FOnBgException
write FOnBgException;
end;
TFtpClient = class(TCustomFtpCli)
protected
FTimeout : Integer; { Given in seconds }
FTimeStop : LongInt; { Milli-seconds }
FMultiThreaded : Boolean;
function Progress : Boolean; override;
function Synchronize(Proc : TFtpNextProc) : Boolean; virtual;
function WaitUntilReady : Boolean; virtual;
public
constructor Create(AOwner: TComponent); override;
function Open : Boolean;
function User : Boolean;
function Pass : Boolean;
function Connect : Boolean;
function Cwd : Boolean;
function Pwd : Boolean;
function CDup : Boolean;
function TypeSet : Boolean;
function TypeBinary : Boolean;
function TypeAscii : Boolean;
function Get : Boolean;
function Put : Boolean;
function RestPut : Boolean;
function RestartPut : Boolean;
function Quit : Boolean;
function Abort : Boolean;
function Receive : Boolean;
function Transmit : Boolean;
function Append : Boolean;
function AppendFile : Boolean;
function Dir : Boolean;
function Directory : Boolean;
function Ls : Boolean;
function List : Boolean;
function Mkd : Boolean;
function Mkdir : Boolean;
function Ren : Boolean;
function Rename : Boolean;
function Dele : Boolean;
function Delete : Boolean;
function Rmd : Boolean;
function Rmdir : Boolean;
function Syst : Boolean;
function System : Boolean;
function Size : Boolean;
function FileSize : Boolean;
function Quote : Boolean;
function DoQuote : Boolean;
function RestGet : Boolean;
function RestartGet : Boolean;
published
property Timeout : Integer read FTimeout write FTimeout;
property MultiThreaded : Boolean read FMultiThreaded write FMultiThreaded;
property HostName;
property Port;
property UserName;
property PassWord;
property HostDirName;
property HostFileName;
property LocalFileName;
property DisplayFileFlag;
property Binary;
property ErrorMessage;
property ShareMode;
property Options;
property ConnectionType;
property ProxyServer;
property SocksPassword;
property SocksPort;
property SocksServer;
property SocksUserCode;
property OnDisplay;
property OnDisplayFile;
property OnCommand;
property OnResponse;
property OnProgress;
property OnSessionConnected;
property OnSessionClosed;
property OnRequestDone;
property OnStateChange;
property OnReadyToTransmit;
property OnBgException;
end;
procedure Register;
implementation
uses WinSock;
{$IFNDEF WIN32}
const
HFILE_ERROR = $FFFF;
{$ENDIF}
{$B-} { Do not evaluate boolean expressions more than necessary }
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure Register;
begin
RegisterComponents('FPiette', [TFtpClient]);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{$IFNDEF WIN32}
procedure SetLength(var Str : String; Len : Integer);
begin
Str[0] := chr(Len);
end;
{$ENDIF}
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{$IFDEF VER80}
function TrimLeft(Str : String) : String;
var
i : Integer;
begin
if Str[1] <> ' ' then
Result := Str
else begin
i := 1;
while (i <= Length(Str)) and (Str[i] = ' ') do
i := i + 1;
Result := Copy(Str, i, Length(Str) - i + 1);
end;
end;
{$ENDIF}
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function GetFileSize(const FileName: string): LongInt;
var
SearchRec: TSearchRec;
begin
if FindFirst(ExpandFileName(FileName), faAnyFile, SearchRec) = 0 then begin
Result := SearchRec.Size;
SysUtils.FindClose(SearchRec);
end
else
Result := -1;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ Step over blank spaces }
function StpBlk(Data : PChar) : PChar;
begin
Result := Data;
if Result <> nil then begin
while (Result^ <> #0) and (Result^ in [' ', #9, #13, #10]) do
Inc(Result);
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function GetInteger(Data : PChar; var Number : LongInt) : PChar;
var
bSign : Boolean;
begin
Number := 0;
Result := StpBlk(Data);
if (Result = nil) then
Exit;
{ Remember the sign }
if Result^ in ['-', '+'] then begin
bSign := (Result^ = '-');
Inc(Result);
end
else
bSign := FALSE;
{ Convert any number }
while (Result^ <> #0) and (Result^ in ['0'..'9']) do begin
Number := Number * 10 + ord(Result^) - ord('0');
Inc(Result);
end;
{ Correct for sign }
if bSign then
Number := -Number;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function GetQuotedString(Data : PChar; var Dst : String) : PChar;
begin
Dst := '';
Result := StpBlk(Data);
if (Result = nil) then
Exit;
if Result^ <> '"' then
Exit;
Inc(Result);
while Result^ <> #0 do begin
if Result^ <> '"' then
Dst := Dst + Result^
else begin
Inc(Result);
if Result^ <> '"' then
Break;
Dst := Dst + Result^;
end;
Inc(Result);
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{* * * *}
{* * TCustomFtpCli * *}
{* * * *}
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
constructor TCustomFtpCli.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FWindowHandle := AllocateHWnd(WndProc);
FOnDisplay := nil;
FOnDisplayFile := nil;
FType := 'I';
FPort := 'ftp';
FState := ftpReady;
FShareMode := fmShareExclusive;
FConnectionType:= ftpDirect;
FProxyServer := ''; // Should Socks properties be set to '' as well?
FOptions := [ftpAcceptLF];
FControlSocket := TWSocket.Create(Self);
FControlSocket.OnSessionConnected := ControlSocketSessionConnected;
FControlSocket.OnDataAvailable := ControlSocketDataAvailable;
FControlSocket.OnSessionClosed := ControlSocketSessionClosed;
FControlSocket.OnDnsLookupDone := ControlSocketDnsLookupDone;
FDataSocket := TWSocket.Create(Self);
FStreamFlag := False;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
destructor TCustomFtpCli.Destroy;
begin
DeallocateHWnd(FWindowHandle);
{ Be sure to have LocalStream closed }
FStreamFlag:=False;
DestroyLocalStream;
inherited Destroy;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TCustomFtpCli.WndProc(var MsgRec: TMessage);
begin
try
with MsgRec do begin
case Msg of
WM_FTP_REQUEST_DONE : WMFtpRequestDone(MsgRec);
WM_FTP_SENDDATA : WMFtpSendData(MsgRec);
else
Result := DefWindowProc(Handle, Msg, wParam, lParam);
end;
end;
except
on E:Exception do
HandleBackGroundException(E);
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ All exceptions *MUST* be handled. If an exception is not handled, the }
{ application will be shut down ! }
procedure TCustomFtpCli.HandleBackGroundException(E: Exception);
var
CanAbort : Boolean;
begin
CanAbort := TRUE;
{ First call the error event handler, if any }
if Assigned(FOnBgException) then begin
try
FOnBgException(Self, E, CanAbort);
except
end;
end;
{ Then abort the component }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -