xHarbour Reference Documentation > Class Reference (textmode) xHarbour Developers Network  

TIpClientFtp()

Creates a new TIpClientFtp object.

Syntax

TIpClientFtp():new( <cUrl>, [<lTrace>] ) --> oTIpClientFtp

Arguments

<cUrl>
This is a character string holding the URL of the FTP server used to upload or download files to/from an FTP server. It must be coded in this form:
   ftp://<userID>:<password>@<server.com>

Alternatively, a TUrl() object can be passed that is initialized with the FTP server URL.

<lTrace>
This parameter defaults to .F. (false). When .T. (true) is passed, the communication with a mail server is logged into the file Ftp<nn>.log, where <nn> is the number of the log file storing information of the internet communictaion with this object.

Return

The function returns a new TIpClientFtp object and method :new() initializes the object.

Description

Objects of the TIpClientFtp() class inherit from the generic internet client class TIpClient(). TIpClientFtp objects are used to communicate with an FTP server. They use the File Transfer Protocol (FTP, RFC0821.TXT) for exchanging files between a local and a remote station. The address of the FTP server must be provided as a URL string with method :new(). The URL is maintained by a TUrl() object, which is stored in the :oUrl instance variable.

The internet connection to the FTP server must be established with the :open() method.

Once the internet connection is established (opened), files can be uploaded or downloaded using the :uploadFile() or :uploadFile() methods.

When all files are sent or received, the internet connection must be closed with the :close() method.

Instance variables

:nConnTimeout
Numeric timeout value in milliseconds.
:nDefaultPort
Numeric port number.

Directory methods

:cwd( <cPath> ) --> lSuccess
Changes the working directory on the FTP server.
:mkd( <cPath> ) --> lSuccess
Creates a directory on the FTP server.
:pwd() --> lSuccess
Queries the current directory on the FTP server.
:rmd( <cPath> ) --> lSuccess
Removes a directory on the FTP server.

File methods

:dele( <cFileName> ) --> lSuccess
Deletes a file on the FTP server
:list( [<cFileSpec>] ) --> cFileList
Retrieves file information from the FTP server as character string.
:listFiles( [<cFileSpec>] ) --> aFiles
Retrieves file information from the FTP server as array.
:downloadFile( <cLocalFile>, [<cRemoteFile>] ) --> lSuccess
Downloads a file.
:mget( <cFileSpec>, <cLocalDir> ) --> cFileList
Downloads multiple files from the FTP server.
:mput( <cFileSpec>, [<cAttr>] ) --> nFileCount
Uploads multiple files to the FTP server.
:rename( <cFrom>, <cTo> ) --> lSuccess
Renames a file on the FTP server.
:uploadFile( <cLocalFile>, [<cRemoteFile>] ) --> lSuccess
Downloads a file.

Info

See also:TIpClient(), TIpClientHttp(), TUrl()
Category: Internet functions , Object functions , xHarbour extensions
Source:tip\ftpcln.prg
LIB:lib\xhb.lib
DLL:dll\xhb.dll

Example

// The example demonstrates file transfers using an USB memory stick connected
// to a WLAN server. The internet address of the WLAN server is 192.168.178.1.

   #include "Directry.ch"

   PROCEDURE Main
      LOCAL oFtp, cRoot
      LOCAL cUSBStick := "ftp://anonymous:guest@192.168.178.1"

      CLS

      oFtp := TIpClientFtp():new( cUSBStick )

      // function for displaying progress bar
      oFtp:exGauge := ( @FtpProgress() )

      IF .NOT. oFtp:open()
         ? oFtp:lastErrorMessage()
         QUIT
      ENDIF

      aFiles := oFtp:listFiles()
      cRoot  := aFiles[1,1]

      ? oFtp:cwd( cRoot    ), oFtp:cReply
      ? oFtp:mkd( "newdir" ), oFtp:cReply
      ? oFtp:cwd( "newdir" ), oFtp:cReply

      ? oFtp:uploadFile( "testftp.prg" )  , oFtp:cReply
      ? oFtp:uploadFile( "testftp.exe" )  , oFtp:cReply

      oFtp:cwd( ".." )
      Dirlist( oFtp, aFiles, "" )
      oFtp:cwd( cRoot + "/newdir"  )

      ? oFtp:downloadFile( "testftp.prg" ), oFtp:cReply

      ? oFtp:dele( "testftp.exe" ), oFtp:cReply
      ? oFtp:dele( "testftp.prg" ), oFtp:cReply

      ? oFtp:cwd( ".." ), oFtp:cReply

      ? oFtp:rmd( "newdir" ), oFtp:cReply

      oFtp:close()

   RETURN

   // Recurses through all directories and lists files
   FUNCTION DirList( oFtp, aFiles, cIndent )
      LOCAL aFile

      FOR EACH aFile IN aFiles
         IF aFile[F_NAME] == "." .OR. aFile[F_NAME] == ".."
            LOOP
         ENDIF

         ? cIndent, aFile[F_NAME]

         IF aFile[F_ATTR][1] == "d"
            oFtp:cwd( aFile[F_NAME] )
            DirList( oFtp, oFtp:listFiles(), cIndent+"   " )
            oFtp:cwd( ".." )
         ENDIF
      NEXT
   RETURN .T.

   // Displays a progress bar during file upload/download
   FUNCTION FtpProgress( nSent, nTotal, oFtp )
      LOCAL cProgress
      LOCAL nRow := Row(), nCol := Col()

      cProgress := Replicate( Chr(178), Int( MaxCol()*nSent/nTotal ) )
      SetPos( MaxRow()-1, 0 )

      ?? oFtp:oUrl:cFile
      ?  PadR( cProgress, MaxCol() )

      SetPos( nRow, nCol )
   RETURN .T.

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe