页面导航:首页 > 软件编程 > Delphi > DownloadafilefromaFTPServer


来源: 作者: 时间:2016-01-21 09:33 【

{**************************************************************} The following function shows how to connect to a ftp server and download a file. Specify the Host, User, Password, Port
  The following function shows how to connect to a ftp server
  and download a file.

  Specify the Host, User, Password, Port, Ftp-Directory,
  File on Ftp, the local file.
  You need a TProgressBar to show the progress and a TLabel to
  show some progress informations.

  Note: The functions from wininet.dll are used.


  WinInet, ComCtrls;

function FtpDownloadFile(strHost, strUser, strPwd: string;
  Port: Integer; ftpDir, ftpFile, TargetFile: string; ProgressBar: TProgressBar): Boolean;

  function FmtFileSize(Size: Integer): string;
    if Size >= $F4240 then
      Result := Format(%.2f, [Size / $F4240]) + Mb
    if Size < 1000 then
      Result := IntToStr(Size) + bytes
      Result := Format(%.2f, [Size / 1000]) + Kb;

  READ_BUFFERSIZE = 4096;  // or 256, 512, ...
  hNet, hFTP, hFile: HINTERNET;
  buffer: array[0..READ_BUFFERSIZE - 1] of Char;
  bufsize, dwBytesRead, fileSize: DWORD;
  sRec: TWin32FindData;
  strStatus: string;
  LocalFile: File;
  bSuccess: Boolean;
  Result := False;

  { Open an internet session }
  hNet := InternetOpen(Program_Name, // Agent
                        INTERNET_OPEN_TYPE_PRECONFIG, // AccessType
                        nil,  // ProxyName
                        nil, // ProxyBypass
                        0); // or INTERNET_FLAG_ASYNC / INTERNET_FLAG_OFFLINE

    Agent contains the name of the application or
    entity calling the Internet functions

  { See if connection handle is valid }
  if hNet = nil then
    ShowMessage(Unable to get access to WinInet.Dll);

  { Connect to the FTP Server }
  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
    ShowMessage(Format(Host "%s" is not available,[strHost]));

  { Change directory }
  bSuccess := FtpSetCurrentDirectory(hFTP, PChar(ftpDir));

  if not bSuccess then
    ShowMessage(Format(Cannot set directory to %s.,[ftpDir]));

  { Read size of file }
  if FtpFindFirstFile(hFTP, PChar(ftpFile), sRec, 0, 0) <> nil then
    fileSize := sRec.nFileSizeLow;
    // fileLastWritetime := sRec.lastWriteTime
  end else
    ShowMessage(Format(Cannot find file ,[ftpFile]));

  { Open the file }
  hFile := FtpOpenFile(hFTP, // Handle to the ftp session
                       PChar(ftpFile), // filename
                       GENERIC_READ, // dwAccess
                       FTP_TRANSFER_TYPE_BINARY, // dwFlags
                       0); // This is the context used for callbacks.

  if hFile = nil then

  { Create a new local file }
  AssignFile(LocalFile, TargetFile);
  Rewrite(LocalFile, 1);

  if IOResult <> 0 then

  dwBytesRead := 0;
  bufsize := READ_BUFFERSIZE;

  while (bufsize > 0) do

    if not InternetReadFile(hFile,
                            @buffer, // address of a buffer that receives the data