TIdFTP.GET, 引用远程文件夹中文件的正确方法是什么?

TIdFTP.GET, What is the correct way to refer to the file in the remote folder?

我正在尝试使用 TIdFTP.Get(版本 10)从我拥有帐户的远程服务器下载文件 (ssleay32.dll)。

该文件确实存在于服务器上的文件夹中,我可以使用 TIdFTP 连接 OK。但是,无论我如何引用文件路径 TIdFTP.Get 都会不断出现错误,无法打开:没有这样的文件或目录。

如果我尝试先将目录更改为文件所在的目录,然后仅使用文件名,我在尝试更改目录时会遇到相同的错误。

引用文件以正确下载的正确方法是什么?

有关信息,当我使用 Filezilla 和与 TIdFTP 使用的相同凭据登录服务器时,Filezilla 似乎将路径显示为 /MAMbase。我已尝试使用 /MAMbase、../MAMbase、./MAMbase、/MAMbase/ 和其他变体,但均未成功。

我正在使用的代码,设置主机的连接参数后,post,用户名和密码如下。使用连接中的路径并仅使用文件名获取或使用完整路径获取与我使用的任何路径格式都会出现相同的错误。

if (not fileexists(pathToCommonFiles + 'libeay32.dll'))  or (not fileexists(pathToCommonFiles + 'ssleay32.dll')) then
     begin
     try
       try
       IdFTP1.Connect ;
  //   IdFTP1.ChangeDir('MAMbase/');
  //   IdFTP1.Get('ssleay32.dll', pathToCommonFiles + 'libeay32.dll',true); 
       IdFTP1.Get('./MAMbase/ssleay32.dll',pathToCommonFiles + 'ssleay32.dll',true); 
       except on E: Exception do
            showmessage ( 'Could not make ftp connection for SSL files'  +slinebreak +
                          'Exception class name = '+E.ClassName+ slinebreak
                       +  'Exception message = '+E.Message);
       end;
     finally
        IdFTP1.quit ;
     end;

     end;

我已经看过类似的问题 here and here 但我没有进一步提出。

(顺便说一句,pathToCommonFiles 包含一个尾部斜杠并且它存在)

首先,您应该提前准备好本地文件名:

LocalLibeayFile := IncludeTrailingPathDelimiter(pathToCommonFiles) + 'libeay32.dll';
LocalSsleayFile := IncludeTrailingPathDelimiter(pathToCommonFiles) + 'ssleay32.dll';

或:

LocalLibeayFile := TPath.Combine(pathToCommonFiles, 'libeay32.dll');
LocalSsleayFile := TPath.Combine(pathToCommonFiles, 'ssleay32.dll');

那么,以下任何一个都应该有效:

if (not FileExists(LocalLibeayFile)) or (not FileExists(LocalSsleayFile)) then
begin
  ...
  IdFTP1.ChangeDir('/MAMbase');
  IdFTP1.Get('libeay32.dll', LocalLibeayFile, True);
  IdFTP1.Get('ssleay32.dll', LocalSsleayFile, True);
  ...
end;
if (not FileExists(LocalLibeayFile)) or (not FileExists(LocalSsleayFile)) then
begin
  ...
  IdFTP1.Get('/MAMbase/libeay32.dll', LocalLibeayFile, True);
  IdFTP1.Get('/MAMbase/ssleay32.dll', LocalSsleayFile, True);
  ...
end;

假设服务器最初将您放入 / 文件夹,您可以省略前导 /:

IdFTP1.ChangeDir('MAMbase');
IdFTP1.Get('libeay32.dll', LocalLibeayFile, True);
IdFTP1.Get('ssleay32.dll', LocalSsleayFile, True);
IdFTP1.Get('MAMbase/libeay32.dll', LocalLibeayFile, True);
IdFTP1.Get('MAMbase/ssleay32.dll', LocalSsleayFile, True);

如果您仍然收到错误,那么您需要确定错误的实际来源 - FTP 服务器或本地计算机。提出的 Exception 的实际 class 类型是什么?如果是 EIdReplyRFCError,则错误来自 FTP 服务器。否则,错误来自本地机器。