nmftp.pas
来自「DELPHI里面一些常用的控件」· PAS 代码 · 共 1,630 行 · 第 1/5 页
PAS
1,630 行
Replymess := Transaction(Cont_Port + GetLocalAddress + DataSocket.GetPortString); {Send Port for data socket}
if (ReplyNumber > 300) and (ReplyNumber < 600) then
if not Assigned(FOnFailure) then
raise FTPException.Create(Replymess)
{Raise exception on errors}
else {_ NOT if not assigned(FOnFailure) then _}
begin
Handled := False;
FOnFailure(Handled, cmdDownload);
if not Handled then
raise FTPException.Create(Replymess)
else goto CleanUp;
{Raise exception on errors}
end; {_ NOT if not assigned(FOnFailure) then _}
end { not _FPassive_ };
StatusMessage(Status_Informational, Cont_Retr + RemoteFile); {Show Outgoing Message}
FBytesTotal := 0;
Replymess := inherited Transaction(Cont_Retr + RemoteFile);
if (ReplyNumber > 300) and (ReplyNumber < 600) then
if not Assigned(FOnFailure) then raise FTPException.Create(Replymess)
{Raise exception on errors}
else {_ NOT if not assigned(FOnFailure) then raise FTPException.Create(Replymess) _}
begin
Handled := False;
FOnFailure(Handled, cmdDownload);
if not Handled then raise FTPException.Create(Replymess)
else goto CleanUp;
{Raise exception on errors}
end; {_ NOT if not assigned(FOnFailure) then raise FTPException.Create(Replymess) _}
FBytesTotal := GetBytesTotal(Replymess);
while (Replymess[1] = ' ') or (Replymess[4] = '-') do
begin
Replymess := Readln; {Handle Extra Lines}
StatusMessage(Status_Informational, Replymess); {Show Received Lines}
if FBytesTotal = 0 then FBytesTotal := GetBytesTotal(Replymess);
if (ReplyNumber > 300) and (ReplyNumber < 600) then
begin
if Assigned(FOnUnSupportedFunction) and (ReplyNumber >= 500) and (ReplyNumber <= 502) then FOnUnSupportedFunction(cmdDownload);
if not Assigned(FOnFailure) then
raise FTPException.Create(Replymess)
{Raise exception on errors}
else {_ NOT if not assigned(FOnFailure) then _}
begin
Handled := False;
FOnFailure(Handled, cmdDownload);
if not Handled then
raise FTPException.Create(Replymess);
{Raise exception on errors}
end; {_ NOT if not assigned(FOnFailure) then _}
end; {_ if (ReplyNumber > 300) and (ReplyNumber < 600) then _}
end; {_ while (Replymess[1] = ' ') or (Replymess[4] = '-') do _}
if not FPassive then
begin
Tsck := DataSocket.handle;
DataSocket.ThisSocket := DataSocket.Accept; {Accept the datasocket}
WinSock.CloseSocket(Tsck);
end;
FBytesTotal := GetBytesTotal(Replymess);
if Assigned(FTransactionStart) then FTransactionStart(self);
if not (BeenCanceled or BeenTimedOut) then
if LocalFile = '' then DataSocket.CaptureFile(RemoteFile)
{If no Local filename specified save file same as remote}
else {_ NOT if LocalFile = '' then DataSocket.CaptureFile(RemoteFile) _} DataSocket.CaptureFile(LocalFile); {If Local filename specified save file under it}
if Assigned(FTransactionStop) then FTransactionStop(self);
DataSocket.RequestCloseSocket;
FBytesTotal := DataSocket.BytesRecvd;
StatusMessage(Status_Informational, (sFTP_Msg_Recvd + IntToStr(BytesTotal) + sFTP_No_Bytes));
if not (BeenCanceled or BeenTimedOut) then Replymess := Readln;
//if ReplyMess='' then ReplyMess:='226 Data Transfer successful';
StatusMessage(Status_Informational, Replymess);
if not (BeenCanceled or BeenTimedOut) then ReadExtraLines(Replymess);
{Read Extra Lines}
{If no Local filename specified save file same as remote}
if Assigned(FTransactionStop) then FTransactionStop(self);
Success := True;
CleanUp:
finally
DataSocket.Destroy; { _Destroy datasocket_ }
DataSocket := nil;
if BeenCanceled then
begin
BeenCanceled := False;
Replymess := Transaction('ABOR');
end;
if Success then if Assigned(FOnSuccess) then FOnSuccess(cmdDownload);
end {_ try _}
end; { _Connected_ }
except
Handled := False;
FOnFailure(Handled, cmdDownload);
if not Handled then
raise;
end;
end;
{*******************************************************************************************
Download a file from a Remote Server
********************************************************************************************}
procedure TNMFTP.DownloadRestore(RemoteFile, LocalFile: string);
var
Replymess: string;
Success, Handled: Boolean;
AFileStream: TFileStream;
Posn: integer;
Tsck: TSocket;
label CleanUp;
begin
Success := False;
BeenCanceled := False; {If there is a cancelled process reset it}
CertifyConnect; {Make sure Connection exists}
if Connected then
begin
if DataAvailable then Read(0);
DataSocket := TPowersock.Create(self); {Create a Data socket}
DataSocket.TimeOut := TimeOut;
if LocalFile = '' then LocalFile := RemoteFile;
if Assigned(FPacketRecvd) then
DataSocket.OnPacketRecvd := FPacketRecvd; {Set function to handle data socket status}
try
if FPassive then
begin
Replymess := Transaction('PASV');
if (ReplyNumber > 499) then
if not Assigned(FOnFailure) then
raise FTPException.Create(Replymess)
else
begin
Handled := False;
FOnFailure(Handled, cmdDownload);
if not Handled then
raise FTPException.Create(Replymess)
else goto CleanUp;
{Raise exception on errors}
end; {_ NOT if not assigned(FOnFailure) then _}
DataSocket.Port := StrToInt(Copy(NthWord(Replymess, ',', 6), 1, Pos(')', NthWord(Replymess, ',', 6)) - 1)) + (256 * StrToInt(NthWord(Replymess, ',', 5)));
DataSocket.Host := Host;
DataSocket.Connect;
end
else { _FPassive_ }
begin
DataSocket.Port := 0; {Set Port to Zero}
DataSocket.Listen(True); {Listen in the datasocket}
Replymess := Transaction(Cont_Port + GetLocalAddress + DataSocket.GetPortString); {Send Port for data socket}
if (ReplyNumber > 300) and (ReplyNumber < 600) then
if not Assigned(FOnFailure) then
raise FTPException.Create(Replymess)
{Raise exception on errors}
else {_ NOT if not assigned(FOnFailure) then _}
begin
Handled := False;
FOnFailure(Handled, cmdDownload);
if not Handled then
raise FTPException.Create(Replymess)
else goto CleanUp;
{Raise exception on errors}
end; {_ NOT if not assigned(FOnFailure) then _}
end { not _FPassive_ };
try
AFileStream := TFileStream.Create(LocalFile, fmOpenRead); {If Local filename specified save file under it}
Posn := AFileStream.Size;
AFileStream.free;
except
Posn := 0;
end;
StatusMessage(Status_Informational, Cont_Rest + Cont_Retr + RemoteFile); {Show Outgoing Message}
FBytesTotal := 0;
Replymess := inherited Transaction(Cont_Rest + IntToStr(Posn));
Replymess := inherited Transaction(Cont_Retr + RemoteFile);
if (ReplyNumber > 300) and (ReplyNumber < 600) then
if not Assigned(FOnFailure) then raise FTPException.Create(Replymess)
{Raise exception on errors}
else {_ NOT if not assigned(FOnFailure) then raise FTPException.Create(Replymess) _}
begin
Handled := False;
FOnFailure(Handled, cmdDownload);
if not Handled then raise FTPException.Create(Replymess);
{Raise exception on errors}
end; {_ NOT if not assigned(FOnFailure) then raise FTPException.Create(Replymess) _}
FBytesTotal := GetBytesTotal(Replymess);
while (Replymess[1] = ' ') or (Replymess[4] = '-') do
begin
Replymess := Readln; {Handle Extra Lines}
StatusMessage(Status_Informational, Replymess); {Show Received Lines}
if FBytesTotal = 0 then FBytesTotal := GetBytesTotal(Replymess);
if (ReplyNumber > 300) and (ReplyNumber < 600) then
begin
if Assigned(FOnUnSupportedFunction) and (ReplyNumber >= 500) and (ReplyNumber <= 502) then FOnUnSupportedFunction(cmdDownload);
if not Assigned(FOnFailure) then
raise FTPException.Create(Replymess)
{Raise exception on errors}
else {_ NOT if not assigned(FOnFailure) then _}
begin
Handled := False;
FOnFailure(Handled, cmdDownload);
if not Handled then
raise FTPException.Create(Replymess);
{Raise exception on errors}
end; {_ NOT if not assigned(FOnFailure) then _}
end; {_ if (ReplyNumber > 300) and (ReplyNumber < 600) then _}
end; {_ while (Replymess[1] = ' ') or (Replymess[4] = '-') do _}
if not FPassive then
begin
Tsck := DataSocket.handle;
DataSocket.ThisSocket := DataSocket.Accept; {Accept the datasocket}
WinSock.CloseSocket(Tsck);
end;
FBytesTotal := GetBytesTotal(Replymess);
if Assigned(FTransactionStart) then FTransactionStart(self);
if not (BeenCanceled or BeenTimedOut) then
if LocalFile = '' then DataSocket.AppendFile(RemoteFile)
{If no Local filename specified save file same as remote}
else {_ NOT if LocalFile = '' then DataSocket.CaptureFile(RemoteFile) _} DataSocket.AppendFile(LocalFile); {If Local filename specified save file under it}
if Assigned(FTransactionStop) then FTransactionStop(self);
DataSocket.RequestCloseSocket;
FBytesTotal := DataSocket.BytesRecvd;
StatusMessage(Status_Informational, (sFTP_Msg_Recvd + IntToStr(BytesTotal) + sFTP_No_Bytes));
if not (BeenCanceled or BeenTimedOut) then Replymess := Read(0);
if Replymess = '' then Replymess := '226 Data Transfer successful';
StatusMessage(Status_Informational, Replymess);
if not (BeenCanceled or BeenTimedOut) then ReadExtraLines(Replymess);
{Read Extra Lines}
{If no Local filename specified save file same as remote}
if Assigned(FTransactionStop) then FTransactionStop(self);
Success := True;
CleanUp:
finally
DataSocket.Destroy; { _Destroy datasocket_ }
DataSocket := nil;
if BeenCanceled then
begin
BeenCanceled := False;
Replymess := Transaction('ABOR');
end;
if Success then if Assigned(FOnSuccess) then FOnSuccess(cmdDownload);
end {_ try _}
end; { _Connected_ }
end;
{*******************************************************************************************
Upload a file to a Remote Server and append to existing file
********************************************************************************************}
procedure TNMFTP.UploadAppend(LocalFile, RemoteFile: string);
var
Replymess: string;
strm: TFileStream;
Success, Handled: Boolean;
Tsck: TSocket;
label CleanUp;
begin
Success := False;
BeenCanceled := False; {If there is a cancelled process reset it}
CertifyConnect; {Make sure Connection exists}
if Connected then
begin
if DataAvailable then Read(0);
DataSocket := TPowersock.Create(self); {Create a Data socket}
DataSocket.TimeOut := TimeOut;
strm := TFileStream.Create(LocalFile, fmOpenRead);
DataSocket.TimeOut := TimeOut;
if Assigned(FPacketSent) then
DataSocket.OnPacketSent := FPacketSent; {Set function to handle data socket status}
try
FBytesTotal := strm.Size;
finally
strm.Destroy;
end; {_ try _}
try
if FPassive then
begin
Replymess := Transaction('PASV');
if (ReplyNumber > 499) then
if not Assigned(FOnFailure) then
raise FTPException.Create(Replymess)
else
begin
Handled := False;
FOnFailure(Handled, cmdAppend);
if not Handled then
raise FTPException.Create(Replymess)
else goto CleanUp;
{Raise exception on errors}
end; {_ NOT if not assigned(FOnFailure) then _}
DataSocket.Port := StrToInt(Copy(NthWord(Replymess, ',', 6), 1, Pos(')', NthWord(Replymess, ',', 6)) - 1)) + (256 * StrToInt(NthWord(Replymess, ',', 5)));
DataSocket.Host := Host;
DataSocket.Connect;
end
else { _FPassive_ }
begin
DataSocket.Port := 0; {Set Port to Zero}
DataSocket.Listen(True); {Listen in the datasocket}
Replymess := Transaction(Cont_Port + GetLocalAddress + DataSocket.GetPortString); {Send Port for data socket}
if (ReplyNumber > 300) and (ReplyNumber < 600) then
if not Assigned(FOnFailure) then
raise FTPException.Create(Replymess)
{Raise exception on errors}
else {_ NOT if not assigned(FOnFailure) then _}
begin
Handled := False;
FOnFailure(Handled, cmdAppend);
if not Handled then
raise FTPException.Create(Replymess)
else goto CleanUp;
{Raise exception on errors}
end; {_ NOT if not assigned(FOnFailure) then _}
end { not _FPassive_ };
if RemoteFile = '' then
begin
StatusMessage(Status_Informational, Cont_Appe + LocalFile); {Show Outgoing Message}
Replymess := Transaction(Cont_Appe + ExtractFileName(LocalFile)) {Give store unique cmmand}
end {_ if RemoteFile = '' then _}
else {_ NOT if RemoteFile = '' then _}
begin
StatusMessage(Status_Informational, Cont_Appe + RemoteFile); {Show Outgoing Message}
Replymess := Transaction(Cont_Appe + RemoteFile); {Give store unique cmmand}
end; {_ NOT if RemoteFile = '' then _}
if (ReplyNumber > 300) and (ReplyNumber < 600) then
begin
if Assigned(FOnUnSupportedFunction) and (ReplyNumber >= 500) and (ReplyNumber <= 502) then FOnUnSupportedFunction(cmdAppend);
if not Assigned(FOnFailure) then
raise FTPException.Crea
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?