PC SOFT

WINDEVWEBDEV AND WINDEV MOBILE
ONLINE HELP

Home | Sign in | English UK
New WINDEV 25 feature!
  • This lesson will teach you the following concepts
  • Overview
  • Connecting to an FTP server
  • Send a file
  • Listing the files found on an FTP server
  • Retrieving a file
  • Disconnecting from an FTP server
Lesson 7.8. The FTP
This lesson will teach you the following concepts
  • Presenting the FTP functions of WINDEV.
Lesson duration

Estimated time: 20 mn
Previous LessonTable of contentsNext Lesson
Overview
The FTP (File Transfer Protocol) is a standard protocol used to transfer files from a computer to another one. One of the computers must be an FTP server.
Several WLanguage commands allow you to transfer files by using this protocol with a server. These programming functions start with "FTP".
WINDEV only proposes "client" functions for the FTP. A standard FTP server is required.
The unit example named "The FTP functions" presents the operations that can be performed on the files found on an FTP server.
  • Open the unit example named "The FTP functions". This example presents the main features that can be used on an FTP server.
Connecting to an FTP server
FTPConnect is used to connect to an FTP server. An FTP account (user name and password) is required to access an FTP server.
  • Display the WLanguage events associated with the "Connection" Button control in "WIN_FTP":
    1. Select the "Connection" Button control.
    2. Press F2 to display the associated WLanguage code:
      // 1 - FTP address: corresponds to the FTP site
      // to which the application must connect. Ex: 192.108.10.2
      sFTPAddress is string = EDT_FTP_Server
      // 2 - User name: if this name is not specified,
      // an "anonymous" connection will be used
      sUserName is string = EDT_User
      sUserPWD is string = EDT_Password
      // Connection to FTP server
      gnConnectionID = FTPConnect(sFTPAddress, sUserName, ...
      sUserPWD)
      IF ErrorOccurred THEN
      // An error occurred during the connection
      Error("The connection to the FTP server: " + ...
      sFTPAddress + " failed", ErrorInfo())
      RETURN
      END
      // The connection is established
      Info("The connection to the FTP server is established", ...
      "connection identifier: " + gnConnectionID)
Remark: You also have the ability to specify the port number for connecting to the FTP server ("21" by default) as well as the connection mode ("True" for a "passive" connection, "False" for an "active" connection).
See Standard FTP functions for more details.
Send a file
To send a file to an FTP server, all you have to do is use FTPSend.
Let's see a code sample that can be used:
// When connecting to the server with FTPConnect, we have
// retrieved the connection number in the gnConnectionID variable
// Transfer the "C:\MyDocuments\File.DOC" file to
// the "Temp" directory found on the server.
bResult is boolean = FTPSend(gnConnectionID, ...
"C:\MyDocuments\File.DOC", "/Temp")

Caution!

Pay great attention to the case (uppercase/lowercase characters) for the name of directories on the server. Indeed, some FTP servers operate under UNIX and are "case sensitive", which means that the case is taken into account for the name of files and directories.
For example, a directory named "MyDirectory" is found on the FTP server. If you try to access "mydirectory", an error such as "Path not found" will be returned by the FTP server because the case is incorrect.
Listing the files found on an FTP server
FTPListFile is used to list the files found on an FTP server. This function is using a "callback" procedure. The procedure is run for each file or directory found.
  • Display the WLanguage events associated with the "List" Button control in "WIN_FTP":
    1. Select the "List" Button control.
    2. Press F2 to display the associated WLanguage code:
      FTPListFile(gnConnectionID,"*.*", ...
      "CallBackFTPListFile", ftpFile+ftpDirectory)

      // Check the function execution
      IF ErrorOccurred THEN
      Error("Error while browsing the files found on the FTP server", ErrorInfo())
      RETURN
      END
    3. In the example, the procedure called is used to display the files found in a trace window.
      PROCEDURE CallBackFTPListFile(sFileName, nFileSize <useful>, ...
      sAttribute, sModifDate <useful>, sModifTime <useful>)
       
      // Is it a file or a directory
      IF sAttribute = "D" THEN
      // Directory or subdirectory
      Trace("Directory: " + sFileName)
      ELSE
      // File
      Trace("File: " + sFileName)
      END
       
      // Continue to browse the files
      RESULT True
Retrieving a file
To get a file from an FTP server, all you have to do is call FTPGet.
Let's see a code sample that can be used:
// When connecting to the server with FTPConnect, we have
// retrieved the connection number in the gnConnectionID variable
// Download the "/Document/File.DOC" file found
// on the FTP server to the "D:\Temp" directory on
// the current computer
bResult is boolean = FTPGet(gnConnectionID, ...
"/Document/File.DOC", "D:\Temp")
Disconnecting from an FTP server
To disconnect from an FTP server, use FTPDisconnect.
  • Display the WLanguage events associated with the "Disconnection" Button control in "WIN_FTP":
    1. Select the "Disconnection" Button control.
    2. Press F2 to display the associated WLanguage code:
      IF YesNo(No, "Do you want to close the connection to the FTP server?") = Yes THEN
      // Disconnect from the FTP server
      FTPDisconnect(gnConnectionID)
      IF ErrorOccurred THEN
      // Error while closing the connection
      Error("Unable to close the FTP connection", ErrorInfo())
      RETURN
      END

      // The connection is ended
      Info("The connection to the FTP server is ended")
      END
Other FTP functions are available but we won't go into details about them in this tutorial. They are mainly used to:
  • create, delete, modify directories on the FTP server,
  • create, delete, modify files on the FTP server,
  • retrieve information about a directory and/or a file,
  • ...
See Standard FTP functions for more details.
Previous LessonTable of contentsNext Lesson
Minimum version required
  • Version 25
Comments
Click [Add] to post a comment