我用Indy的TIdFTPServer编写了FTP服务端程序,并用IE的Ftp可以列出服务器的目录,用D6的FTPDemo和ICS的FTP示例均能列出服务器的目录,但用Indy的FTP的Demos中的FTP客户端示例却不能列出服务器的目录。为此,我看了TidFTP和TIdFTPServer、TIdFTPList的源代码,也没发现问题。现把相应的过程附后,请教各位高手。谢谢!!Client:procedure TFormMain.ViewList;
var
  DirList: TStringList;
  i: Integer;
  ListItem: TListItem;
  Attribute: TIdDirItemType;
begin
  //向ListViewRemote控件逐个添加FTP当前工作目录下的文件目录
  DirList := TStringList.Create;
  try
    try
      IdFTP1.List(DirList);
    except;
    end;
  finally
    DirList.Free;
  end;
  memoinfo.Lines.add(inttostr(IdFTP1.DirectoryListing.Count));
  ListViewRemote.Clear;
  for i := 0 to IdFTP1.DirectoryListing.Count - 1 do
  begin
    ListItem := ListViewRemote.Items.Add();
    ListItem.Caption := IdFTP1.DirectoryListing[i].FileName;
    ListItem.SubItems.Add(IntToStr(IdFTP1.DirectoryListing[i].Size));
    ListItem.SubItems.Add(DateToStr(IdFTP1.DirectoryListing[i].ModifiedDate));
    Attribute := IdFTP1.DirectoryListing[i].ItemType;
    if Attribute = ditDirectory then
    begin
      //本项是目录
      ListItem.SubItems.Add('目录');
      ListItem.ImageIndex := 0
    end
    else if Attribute = ditFile then
    begin
      //本项是文件
      ListItem.SubItems.Add('文件');
      ListItem.ImageIndex := 1
    end
    else if Attribute = ditSymbolicLink then
    begin
      ListItem.SubItems.Add('符号链接');
      ListItem.ImageIndex := 2
    end;
  end;
end;Servser:procedure TMainForm.IdFTPServer1ListDirectory(ASender: TIdFTPServerThread;
  const APath: String; ADirectoryListing: TIdFTPListItems);
var
  item: TIdFTPListItem;
  flag:integer;
  SRec:TSearchRec;
begin
    ADirectoryListing.ListFormat:=flfdos;
    Flag:=FindFirst(Pchar('D:\common\*.*'),faAnyFile,Srec);
    if Flag=0 then
    begin
     item := ADirectoryListing.Add;
     item.FileName := SRec.Name;
     if  Srec.attr=16 then
         item.ItemType := ditDirectory
     else item.itemtype:=ditfile;
     item.Size:=Srec.Size;
     item.ModifiedDate:=FileDateToDateTime(SRec.Time);
    end;
     while (FindNext(SRec) = 0) do
     begin
       item := ADirectoryListing.Add;
       item.FileName := SRec.Name;
       if  Srec.attr=16 then
         item.ItemType := ditDirectory
       else item.itemtype:=ditfile;
       item.Size:=Srec.Size;
       item.ModifiedDate:=FileDateToDateTime(SRec.Time);
     end;
     findclose(Srec);
end;

解决方案 »

  1.   

    最后加上
    ADirectoryListing.BeginUpdate;
    不然 ADirectoryListing.Add;不会生效
      

  2.   

    如果是中文目录乱码问题,解决如下:IE在LIST命令之前会发送一个opts utf8 on 命令启用utf8格式的文件名传输,但TIdFTPServer不支持这个功能,会产生一个202回复。麻烦就在这里,IE忽略了这个202回复并继续以utf8模式处理服务器发送的ANSI数据,导致列表混乱。解决办法是找到CommandOPTS过程,把回复代码改为502或504。
      

  3.   

    CommandOPTS过程在Indy的源代码中,修改后你需要重新编译Indy组件。