Domanda

I am using this function to get the file from ftp connection,

function GetFileFromFTP(server, username, password, localfile, remotefile: string; port: word = 21): boolean;
var
  hopen, hconnect: HINTERNET;
  good:boolean;
begin
  hopen := InternetOpen('myagent', INTERNET_OPEN_TYPE_DIRECT, nil, nil, 0);
  hconnect := InternetConnect(hopen, pchar(server), port, pchar(username), pchar(password), INTERNET_SERVICE_FTP,  INTERNET_FLAG_PASSIVE, 0);
  good := FtpGetFile(hconnect, pchar(remotefile), pchar(localfile), false, 0, FTP_TRANSFER_TYPE_UNKNOWN or INTERNET_FLAG_DONT_CACHE, 0);
  InternetCloseHandle(hconnect);
  Result := good;
end;

The problem is when I use the server string like this:

var server:string;

server := 'ftp://192.168.1.1/XDIRECTORY/'; //IT CANT GET THE FILE
server := 'localhost'; //GETS THE FILE


procedure TForm1.btn1Click(Sender: TObject);
begin
if GetFileFromFTP(server, '', '', 'upx2.exe', 'upx.exe') then
begin
Caption := 'Install succesfull';
end
else
begin
Caption := 'Install NOT succesfull';
end;

I don't understand why can't the ftp server get the file, if the file is in a folder, or if a server ip address will be used.

It will only good if I set the server to localhost

È stato utile?

Soluzione

because server is server and not URI. And URI is URI and not a server. You should separate URI's components as required by the function you chosen to use.

Read https://www.google.ru/search?client=opera&q=MSDN+FtpGetFile&sourceid=opera and determine what is expected in which variable.

Read http://en.wikipedia.org/wiki/URI_scheme#Generic_syntax how to parse URL and extract server name and remote file name into different variables.

In the string ftp://user:password@192.168.1.1:21/XDIRECTORY/YDIRECTORY/ZDIRECTORY/filename only 192.168.1.1 is a "server" - all the rest are different parts and are not the "server". You should extract those parts into proper separate variables and pass them to function as it is documented on MSDN.

GetFileFromFTP(server, '', '', 'upx2.exe', 'upx.exe') - the last two parameters here i believe are wrong - they both should be fully-qualified names, including the paths.


Bonus: reformulation

procedure TForm1.btn1Click(Sender: TObject);
begin
if GetFileFromFTP(server, '', '', 'upx2.exe', 'upx.exe') then
begin
Caption := 'Install succesfull';
end
else
begin
Caption := 'Install NOT succesfull';
end;

is

procedure TForm1.btn1Click(Sender: TObject);
begin
  Caption := 'Install was ' +  
      IfThen( 
          not GetFileFromFTP(server, '', '', 'upx2.exe', 'upx.exe'),
      'NOT ')
   + 'succesfull.';
end;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top