delphi.gif (306 バイト) FTPの使い方


FTPを使うとインターネット経由でファイルのやり取りを行うことができます。

 

toach.gif (917 バイト) FTPサイトに接続するには

RemoteHost,RemotePort,LocalPortプロパティを設定してConnectメソッドを実行します。切断はQuitコマンドを使用します。

{ OLEでパラメータを省略する時使用する }
function TfrmFtpSamp.NoParam: Variant;
begin
  TVarData(Result).VType := varError;
  TVarData(Result).VError:= DISP_E_PARAMNOTFOUND;
end;

{ 接続する }
procedure TfrmFtpSamp.cmdConnectClick(Sender: TObject);
begin
    with FTP1 do begin
        RemoteHost := txtRemoteHost.Text;
        RemotePort := StrToInt(txtRemotePort.Text);
        LocalPort := StrToInt(txtLocalPort.Text);
        Connect(NoParam, NoParam);
        lblConnect.Caption := StateString;
    end;
end;

プロパティ設定例

 

toach.gif (917 バイト) ホストの認証を得るには

接続ができたらホストにユーザIDとパスワードを送って認証を受けなければなりません。認証には、匿名ID(anonymous)を使える場合もあります。

ProtocolStateChangedイベントでProtocolStateがftpAuthenticationになったら、Authenticateメソッドで認証を受けます。

{ プロトコルの状態が変化するとき }
procedure TfrmFtpSamp.FTP1ProtocolStateChanged(Sender: TObject;
  ProtocolState: Smallint);
begin
    case ProtocolState of
    ftpAuthentication:
        begin
            FTP1.Authenticate(txtUserID.Text, txtPassword.Text);  // 認証
            lblConnect.Caption := '認証中';
        end;

    ftpTransaction:
        begin
            lblConnect.Caption := '認証が成功した(接続)';
        end;
    end;

end;


toach.gif (917 バイト) ファイルを送る(受け取る)には

FTPサイトへファイルを送るにはPutFileメソッドを使います。ファイルを受け取るにはGetFileメソッドを使用します。

{ ファイル転送を行なう }
procedure TfrmFtpSamp.cmdPutClick(Sender: TObject);
begin
    FTP1.PutFile(txtFileName.Text, txtRemoteFile.Text);
    lblConnect.Caption := FTP1.ReplyString;
end;

{ ファイル取得を行なう }
procedure TfrmFtpSamp.cmdGetClick(Sender: TObject);
begin
    FTP1.OleObject.GetFile(txtRemoteFile.Text, txtFileName.Text);
    lblConnect.Caption := FTP1.ReplyString;
end;


toach.gif (917 バイト) ファイルの一覧を得るには

FTPサイトのファイル一覧を得るにはListメソッドを使います。この例は\Delphi3\Demos\Internet\Ftpにあります。

 

Delphi4 パワーガイド」の「6-17 FTPを使うには」も参考にしてください。