Ago
Ago

Reputation: 765

How to connect to FTP using ftp://userName@host

I'm having trouble connecting to the FTP sever. Most of the time I connect to an FTP server like this :

// ftp is TidFTP
ftp.Host := '119.xxx.xxx.133';
ftp.Username := 'fnc';
ftp.Password := 'fnc';
ftp.Port := 21;
ftp.ConnectTimeout := 5000;
ftp.Connect;

Our administrator gave me a link like this

ftp://[email protected]/Files/

to access the FTP. User name is fnc, port is 21, password is fnc.

If I access the FTP via Windows Explorer, I don't get any errors, I can put files flawlessly. But if I do it in code I get illegal port command errors whenever I try to put files.

Note that I can connect to the ftp server using the code above but can't put any files there. Thanks in advance.

Upvotes: 2

Views: 2617

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 595402

Given an FTP URL, you need to parse the URL and assign its components to the various TIdFTP properties and methods, eg:

var
  URL: string;
  Uri: TIdURI;
begin
  ...
  URL := ...; // 'ftp://fnc:[email protected]/Files/'
  Uri := TIdURI.Create(URL);
  try
    ftp.Host := Uri.Host;
    if Uri.Port <> '' then
      ftp.Port := StrToInt(Uri.Port)
    else
      ftp.Port := 21;
    ftp.Username := Uri.Username;
    ftp.Password := Uri.Password;
    ftp.ConnectTimeout := 5000;
    ftp.Connect;
    if Uri.Path <> '/' then
      ftp.ChangeDir(Uri.Path);
    ...
  finally
    Uri.Free;
  end;
  ...
end;

Upvotes: 4

Ago
Ago

Reputation: 765

ftp.passive := True

is the answer. Thansk to Sir Rufo.

Upvotes: 0

Related Questions