|
|
|
|
- In this lesson you will learn the following concepts
- Overview
- Connecting to an FTP server
- Sending a file
- Listing files on an FTP server
- Retrieving a file
- Disconnecting from an FTP server
In this lesson you will learn the following concepts - Overview of WINDEV's FTP functions.
FTP (File Transfer Protocol) is a standard protocol for transferring files from one computer to another. One of the computers must be an FTP server. Multiple WLanguage functions allow you to transfer files by using this protocol with a server. These functions start with "FTP". WINDEV only includes FTP "client" functions. A standard FTP server is required. The unit example named "The FTP functions" shows how to handle files on an FTP server.
- Open the "The FTP functions" unit example. This example presents the main functionalities 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 (username and password) is required to access an FTP server. - Open the WLanguage events associated with the "Connection" Button control in "WIN_FTP":
- Select the "Connection" Button control.
- Press F2 to open 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 - Username: 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("Connection to the FTP server established", "Connection identifier: " + gnConnectionID)
Remark: You can also specify the port number of the FTP server connection ("21" by default) as well as the connection mode ("True" for a "passive" connection, "False" for an "active" connection). For more details, see Standard FTP functions. To send a file to an FTP server, use FTPSend. Let's see an example of code 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 close attention to the case (uppercase/lowercase characters) of the directory names on the server. Some FTP servers are UNIX machines and are "case sensitive", i.e. they are sensitive to the case of file and directory names. For example, there is a directory named "MyDirectory" 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 files on an FTP server FTPListFile lists the files on an FTP server. This function uses a "callback" procedure. The procedure is run for each file or directory found. - Open the WLanguage events associated with the "List in a trace" Button control in "WIN_FTP":
- Select the "List in a trace" Button control.
- Press F2 to open 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
- In the example, CallBackFTPListFile is an internal procedure that displays in a trace window the files found.
INTERNAL 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 loop through the files RETURN True
To get a file from an FTP server, call FTPGet. Let's see an example of code 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 bRes is boolean = FTPGet(gnConnectionID, "C:\MyDocuments\File.DOC", "/Temp")
Disconnecting from an FTP server To disconnect from an FTP server, use FTPDisconnect. - Open the WLanguage events associated with the "Disconnection" Button control in "WIN_FTP":
- Select the "Disconnection" Button control.
- Press F2 to open 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 over") 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 and change directories on the FTP server,
- create, delete and change files on the FTP server,
- retrieve information about a directory and/or a file,
- ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|