Download a File from A FTP Server

zhaozj2021-02-16  51

{********************************************************** ************} 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 Fun Some Progress INFORMATIONS. NOT: The functions from wininet.dll area used. {********************** ************************************************************} Uses Wininet, Comctrls; Function FtpDownloadFile (strHost, strUser, strPwd: string; Port: Integer; ftpDir, ftpFile, TargetFile: string; ProgressBar: TProgressBar): Boolean; function FmtFileSize (Size: Integer): string; begin if Size> = $ F4240 then Result: = Format ('%. 2f', [Size / $ F4240]) 'MB' else if size <1000 Then Result: = INTOSTR (SIZE) 'BYTES' ELSE RESULT: = Format ('%. 2f', [size / 1000]) 'KB'; end; const _ b 256, 512, ... var hnet, HFTP, HFile: Hinternet ; Buffer: array [0..READ_BUFFERSIZE - 1] of Char; bufsize, dwBytesRead, fileSize: DWORD; sRec: TWin32FindData; strStatus: string; LocalFile: File; bSuccess: Boolean; begin 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 begin ShowMessage ( 'Unable to get access to WinInet.Dll'); Exit ; end; {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 begin InternetCloseHandle (hNet); ShowMessage (Format ( 'Host "% s" is not available', [strHost]); exit; end; {change directory} Bsuccess: = ftpset CurrentDirectory (hFTP, PChar (ftpDir)); if not bSuccess then begin InternetCloseHandle (hFTP); InternetCloseHandle (hNet); ShowMessage (Format ( '. Can not set directory to% s', [ftpDir])); Exit; end; { Read size of file} if FtpFindFirstFile (hFTP, PChar (ftpFile), sRec, 0, 0) <> nil then begin fileSize: = sRec.nFileSizeLow; // fileLastWritetime: = sRec.lastWriteTime end else begin InternetCloseHandle (hFTP); InternetCloseHandle (HNET); showMessage (Format ('cannot File', [ftpfile]); exit; end; {Open the file} hfile: =

转载请注明原文地址:https://www.9cbs.com/read-27280.html

New Post(0)