该控件提供的GET方法好像只能下载一个文件吧,下载目录则需要遍历目录文件有代码指示下吗? 网上那个代码根本就有错误,好多网站都是抄的同一文章,希望能有一通过的代码,测试通过200全给。

解决方案 »

  1.   

    这个我建议你自己写一下,主要用到递归,idftp.get下载文件,idftp.changedir改变目录,idftp.list取得文件列表,用循环递归就可以完成了
      

  2.   

    给你个我以前写的软件里的几段FTP下载的代码吧,有些地方功能不适合的可以自己修改
    function TForm15.initialFTPCon():string;
    {初始化FTP网络连接}
    begin
      {打开一个Internet会话}
      Result :='ok';
      hNet := InternetOpen('Program_Name ', //   Agent
        INTERNET_OPEN_TYPE_PRECONFIG, //   AccessType
        nil, //   ProxyName
        nil, //   ProxyBypass
        0); //   or   INTERNET_FLAG_ASYNC   /   INTERNET_FLAG_OFFLINE  {查看连接句柄是否有效}
      if hNet = nil then
      begin
        Result := '无法初始化 WinInet.Dll';
        Exit;
      end;  {连接到Ftp服务器}
      hFTP := InternetConnect(hNet, //   Handle   from   InternetOpen
        PChar(strHost), //   FTP   server
        port, //   (INTERNET_DEFAULT_FTP_PORT),
        PChar(StrUser), //   username
        PChar(strPwd), //   password
        INTERNET_SERVICE_FTP, //   FTP,   HTTP,   or   Gopher?
        0, //   flag:   0   or   INTERNET_FLAG_PASSIVE
        0); //   User   defined   number   for   callback  if hFTP = nil then
      begin
        InternetCloseHandle(hNet);
        Result := Format('无法连接到主机: "%s "!', [strHost]);
        Exit;
      end;  {改变目录 }
      bSuccess := FtpSetCurrentDirectory(hFTP, PChar(ftpDir));  if not bSuccess then
      begin
        InternetCloseHandle(hFTP);
        InternetCloseHandle(hNet);
        Result := Format('无法改变目录到:%s. ', [ftpDir]);
        Exit;
      end;  
    end;procedure TForm15.FormCreate(Sender: TObject);
    {列举需要更新的文件}
    var
      hSearch: HINTERNET;
      findData: WIN32_FIND_DATA;
      MyItem: TListItem;
      SearchRec: TSearchRec;
      Directory: string;
    var
      Item: TListItem;
    begin
      Label3.Caption := ExeVer;
      Self.DoubleBuffered := True;
      Label5.Caption := CheckUpdate;
      if label5.Caption = '无法连接到升级服务器...' then
      begin
        BitBtn1.Enabled := False;
        Exit;
      end;  if initialFTPCon <> 'ok' then  Exit;    // 初始化网络连接  bAll := False;
      Directory := ExtractFilePath(Application.ExeName)+'\';
      ListView1.Items.Clear;  // 枚举FTP指定目录下的文件
      hSearch := FtpFindFirstFile(hFTP, '*.*', findData, 0, 0);
      if hSearch <> nil then
      begin
        repeat
          if findData.dwFileAttributes = faDirectory then  Continue;
          
          // 下载FTP里的所有文件
          if bAll = True then
          begin
            MyItem := ListView1.Items.add;
            MyItem.Caption := findData.cFileName;
            MyItem.subitems.add(FmtFileSize(findData.nFileSizeLow));
            MyItem.subitems.add('未下载');
          end else
          begin
          // 下载本地有,但是修改时间不一致的文件
            if FindFirst(Directory + findData.cFileName, faDirectory, SearchRec) = 0 then
            begin
              repeat
                if CovFileDateFTP(findData.ftLastWriteTime) <> CovFileDate(SearchRec.FindData.ftLastWriteTime) then
                begin
                  MyItem := ListView1.Items.add;
                  MyItem.Caption := findData.cFileName;
                  MyItem.subitems.add(FmtFileSize(findData.nFileSizeLow));
                  MyItem.subitems.add('未下载');
                end;
              until FindNext(SearchRec) <> 0;
              FindClose(SearchRec);
            end
            else
            // 下载本地没有的
            begin
              MyItem := ListView1.Items.add;
              MyItem.Caption := findData.cFileName;
              MyItem.subitems.add(FmtFileSize(findData.nFileSizeLow));
              MyItem.subitems.add('未下载');
            end;      end;
        until not InternetFindNextFile(hSearch, @findData);
      end;
      InternetCloseHandle(hFTP);
      InternetCloseHandle(hNet);
    end;procedure TForm15.DownLoadFile(szSrc, szDest: string);
    var
      LocalFile: file;
      buffer: array[0..READ_BUFFERSIZE - 1] of Char;
      bufsize, dwBytesRead, fileSize: DWORD;
    {下载FTP文件函数}
    begin
      initialFTPCon;
      {获取文件大小}
      if FtpFindFirstFile(hFTP, PChar(szSrc), sRec, 0, 0) <> nil then
      begin
        fileSize := sRec.nFileSizeLow;
      end else
      begin
        InternetCloseHandle(hFTP);
        InternetCloseHandle(hNet);
        label7.Caption  :=Format('找不到文件:%s.  ', [szSrc]);
        Exit;
      end;  {打开文件 }
      hFile := FtpOpenFile(hFTP,  PChar(szSrc), GENERIC_READ, FTP_TRANSFER_TYPE_BINARY, 0);
      if hFile = nil then
      begin
        InternetCloseHandle(hFTP);
        InternetCloseHandle(hNet);
        Exit;
      end;  {创建本地文件 }
      AssignFile(LocalFile, szDest);
    {$I-}
      Rewrite(LocalFile, 1);
    {$I+}
      if IOResult <> 0 then
      begin
        InternetCloseHandle(hFile);
        InternetCloseHandle(hFTP);
        InternetCloseHandle(hNet);
        Exit;
      end;  dwBytesRead := 0;
      bufsize := READ_BUFFERSIZE;  while (bufsize > 0) do
      begin
        Application.ProcessMessages;    if not InternetReadFile(hFile,  @buffer,  READ_BUFFERSIZE,  bufsize) then Break;    if (bufsize > 0) and (bufsize <= READ_BUFFERSIZE) then
          BlockWrite(LocalFile, buffer, bufsize);
        dwBytesRead := dwBytesRead + bufsize;    {更新进度条 }
        ProgressBar1.Position := Round(dwBytesRead * 100 / fileSize);
        label7.Caption := '正在下载  '+ szSrc;
        // labee.Caption := Format('文件大小:%s,已经复制 %s,进度:%d %% ', [FmtFileSize(dwBytesRead), FmtFileSize(fileSize), ProgressBar.Position]);
      end;
      CloseFile(LocalFile);
      InternetCloseHandle(hFile);
    end;procedure TForm15.BitBtn1Click(Sender: TObject);
    // 开始下载
    var
      szDest, szNew : string;
      i :Integer;
    begin
        for I := 0 to ListView1.Items.Count - 1 do
        begin
          szName := ListView1.Items[i].Caption;
          szDest := ExtractFilePath(Application.ExeName) +'\'+ szName;
          ListView1.Selected :=ListView1.Items[i];
          DownLoadFile(szName, szDest);
          ListView1.Items[i].SubItems.Strings[1] :='已下载';
        end;
        InternetCloseHandle(hFTP);
        InternetCloseHandle(hNet);
        Label7.Caption := '文件全部下载完成!';
    end    
      

  3.   

    对了 ,需要先定义几个const
    const
      READ_BUFFERSIZE = 16384; 
      strHost = '11.28.250.20';
      strUser = 'anonymous';
      strPwd = '';
      Port = 21;
      ftpDir = 'Downloads\你的目录\';