ColdFusion TFTP Client Sample Source Code
Network Component provides an easy-to-use development interface to a variety of IP protocols. By using Network Component, you can very easily create or enhance applications with network features.
Network Component features the following: DNS, FTP, HTTP, HTTPs, ICMP Ping, IP-to-Country, MSN, NTP, RSH, SCP, SFTP, SNMP v1/v2c (Get, GetNext, Set), SNMP Traps, SNMP MIB, SSH, TCP, Telnet, TFTP, UDP, Telnet, Wake-On-LAN and more.
Network Component can be well integrated into any development platform that supports ActiveX objects.
This document describes how the Network Component FtpServer object can be integrated into your projects.
The most important functions of the FtpServer object are:
- Connect - connect to the (remote) FTP server on port 21 or any alternate port;
- Disconnect - to diconnect after a connect call;
- GetCurrentDir - retrieve the current directory;
- ChangeDir - change the current directory;
- CreateDir - create a new directory;
- RenameDir - rename a directory;
- DeleteDir - delete a directory;
- FindFile - find a specific file in the current directory;
- FindFirstFile - iterate over all files in the current directory; find the first file;
- FindNextFile - iterate over all files in the current directory; find the next file;
- RenameFile - rename a file in the current directory;
- DeleteFile - delete a file in the current directory;
- GetFile - get (download) a file, either using binary transfer or ASCII transfer;
- PutFile - put (upload) a file, either using binary transfer or ASCII transfer;
Step 1: Download and install the Network Component
Download Network Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.
Step 2: Create a new ColdFusion document
Create a new blank webdocument with the ".cfm" extention. First of all we are going to build the form whitch commands and properties of the device can be filled in. Then we are going to make a source code that connects to the device.
Step 3: Implementation
Establishing the connection and listing the files can be done in three steps:
- Collect the ftp-server-address, the username and the password from the user
- Establish the connection
- List the files
Collecting the information can be done using a form. We've made our form look like this:
When we've got this information, we're ready to establish a connection to a server. First of all we need an object witch creates a connection between ColdFusion and the ActiveXperts software. To do that, use the following code:
<cfobject class="AxNetwork.TftpServer" type="com" name="objFtp" Action="Create">
Once the object is created, we're able to use the component. We need to establish a connection, so we're going to use the Network Component method "connect()". Use the following code:
objFtp.Connect(strHost,strUsername,strPassword);
Now we need to list the files. The Network Component is able to get the first file in the folder using the "FindFirstFile()" method and then the next file using the "FindNextFile()" method. So we're going to list the first file in the folder first, then the next file (the second file) and than the next file (the third file) and so on. Use the following code:
objFiles = objFtp.FindFirstFile(); do{ writeoutput ("<tr>"); writeoutput ("<td>"); if(objFiles.IsDirectory()){ writeoutput("Directory:"); } else{ writeoutput("File:"); } writeoutput ("</td>"); writeoutput ("<td>"); writeoutput(objFiles.Name & "<br>"); writeoutput ("</td>"); writeoutput ("</tr>"); objFiles = objFtp.FindNextFile(); } while(objFtp.LastError eq 0);
Perhaps you would like to show the content of another folder. Paste the following code, before the listing code.
objFtp.ChangeDir(strDirectory);
We're displaying the results of the process using the "LastError" method of the Network Component. The method "LastError" displays a number. It's helpfull to know "0" means success. To get the error description use the "GetErrorDescription()" method. In our code we've used these methodes this way:
strResult = objFtp.LastError & " : " & objFtp.GetErrorDescription(objFtp.LastError);
It can be very helpfull to check for errors before executing some code. Applying this can be done easily:
if(objFtp.LastError eq "0"){ Some code }
Appendix: Full source code
<!--- creating the object ---> <cfobject class="AxNetwork.TftpServer" type="com" name="objTftp" Action="Create"> <cfoutput> <html> <head> <title>ActiveXperts Network Component TFTP Sample in ColdFusion</title> <style> <!-- body{ text-align: center; } h2{ font-family: verdana; color: navy; font-weight: bold; } form{ margin: 0px; } .input{ font-family: verdana; font-size: 9pt; color: navy; border: 1px solid navy; background-color: white; width: 325px; } .button{ font-family: verdana; font-size: 9pt; width: 325px; } .table{ font-family: verdana; font-size: 9pt; color: navy; border: 1px solid navy; width: 500px; } div{ width: 325px; height: 50px; overflow: auto; text-align: left; font-size: xx-small; font-family: verdana; border: 1px solid navy; } a{ font-family: verdana; font-size: 8pt; color: red; } --> </style> </head> <body> </cfoutput> <cfscript> strHost = "192.168.31.98"; nPort = 22; strRemoteFileTFTP = ""; strLocalFileWindows = ""; strResult = ""; strLogfile = GetTempDirectory() & "TFTP.log"; strComponent = "Module [" & objTftp.Module & "]; Build [" & objTftp.Build & "]"; if (isDefined("URL.txtHost")){ strHost = URL.txtHost; } if (isDefined("URL.txtPort")){ nPort = URL.txtPort; } if (isDefined("URL.txtRemoteFileTFTP")){ strRemoteFileTFTP = URL.txtRemoteFileTFTP; } if (isDefined("URL.txtLocalFileWindows")){ strLocalFileWindows = URL.txtLocalFileWindows; } if (isDefined("URL.txtLogfile")){ strLogfile = URL.txtLogfile; } if(IsDefined("URL.Submitbutton")){ objTftp.Logfile = strLogfile; objTftp.HostPort = nPort; objTftp.BinaryTransfer = True; objTftp.Get(strRemoteFileTFTP, strLocalFileWindows); strResult = objTftp.LastError & ": " & objTftp.GetErrorDescription(objTftp.LastError); } </cfscript> <cfoutput> </head> <body> <form method=get> <h2>ActiveXperts Network Component CF Sample - TFTP</h2> <table class=table> <tr> <td>Component:</td> <td>#strComponent#</td> </tr> <tr> <td>Host:</td> <td><input class=input type=text name=txtHost value="#strHost#"></td> </tr> <tr> <td>Port:</td> <td><input class=input type=text name=txtPort value="#nPort#"></td> </tr> <tr> <td>Remote File(TFTP):</td> <td><input class=input type=text name=txtRemoteFileTFTP value="#strRemoteFileTFTP#"></td> </tr> <tr> <td>Local File(Windows):</td> <td><input class=input type=text name=txtLocalFileWindows value="#strLocalFileWindows#"></td> </tr> <tr> <td> </td> <td><input class=button type=submit name=submitbutton value="Copy Remote File to Local File"></td> </tr> <tr> <td>Logfile:</td> <td><input class=input type=text name=txtLogfile value="#strLogfile#"></td> </tr> <tr> <td><b>Result: </b></td> <td>#strResult#</td> </tr> </table> </form> <br /> <p>This sample is based on ActiveXperts Network Component, an <a target="blank" href="https://www.activexperts.com">ActiveXperts Software</a> product.</p> <a href="index.cfm">Back to main page</a> </body> </html> </cfoutput>
You can download the complete samples here. There are many other working Network Component scripts on our site and shipped with the product.