Contact Info

Crumbtrail

ActiveXperts.com » Network Component » How to Use Network Component » FTP » Classic ASP

ASP 2.x FTP Client Sample Source Code in ASP

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:


Step 1: Download and install 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 Web Site

First, create a new directory on the IIS Server's file system. This directory will hold the ASP later on.

From the 'Start menu', click on 'Administrative Tools' and click on 'Internet Information Services (IIS) Manager'. Right-click on the 'Web Sites' container and choose 'New->Web Site':

IIS

(Click on the picture to enlarge)

The 'Web Site Creation Wizard' is shown, guiding you thorugh the process of creating a new web site. Provide all necessary information:

You're now able to write an ASP script to use IP protocols with Network Component.

Step 3: Create the from

First of all we need to create a form in HTML to get the login information for the ftp server. All we actually need is:

In this sample all we're going to do is create a ASP file that is able to list files and directories in a directory. Using Network Component it is easy to create a page that makes you able to upload and to download files.

You can create a form that looks like this:

(Click on the picture to enlarge)

Step 4:Create ActiveXperts Network Component objects in ASP

Create a new ASP script called DEFAULT.ASP in the directory that was created in Step2, using your favorite editor. On top of the ASP code, insert the following lines to declare and create the Dns object:

<object runat="server" progid="AxNetwork.FtpServer"   id="objFtpServer"></object>
<object runat="server" progid="AxNetwork.ASConstants" id="objConstants"></object>

Step 5: Test a small piece of ASP

Now, test if your new web site is working well with ActiveXperts Network Component using your browser. If you are using Microsoft Internet Explorer, it is recommended to disable friendly error message because this default setting doesn't show any ASP error message, making it hard to debug if there are any problems:

Now, use the following piece of code in your DEFAULT.ASP page and test it into your favourite browser:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ActiveXperts Network Component ASP Demo</title>
    
    <object runat="server" progid="AxNetwork.FtpServer" id="objFtpServer"></object>
    <object runat="server" progid="AxNetwork.Constants" id="objConstants"></object>

    <title>Network Component Demo</title>
  </head>
  <body>
     Network Component version: <% = objSmtp.Version %>
     <br />
     Network Component License Status: <% = objSmtp.LicenseStatus %>
  </body>
</html>

Step 6: List files

You can list files using Network Component FTP in 3 steps.

  1. Creating an object to use Network Component
  2. Connect to the server
  3. List the files

Connecting to the server

To connect to the server we're using the Network Component method "Connect".

objFtp.connect request("server"), request("username"), request("password")
if request("submitbutton") <> "" then

  dim objFiles

  <span class="aclSourceComment">' create the object</span>
  Set objFtp = Server.CreateObject( "AxNetwork.FtpServer" )
  
  objFtp.clear
  
  <span class="aclSourceComment">' set the logfile</span>
  objFtp.logfile = request("logfile")
  
  <span class="aclSourceComment">' connect</span>
  objFtp.connect request("server"), request("username"), request("password")
  objFtp.ChangeDir request("directory")
  
else
  <span class="aclSourceComment">' do nothing</span>
end if

Listing the files

If the previous actions have been succesfully executed, we can list the files. To list these files we're using two Network Component methods:

  1. We're using "FindFirstFile" to find the first file.
  2. We're using "FindNextFile" to move on to the next.

When we know what the first file is we're starting a loop. If one file is listed, FindNextFile is used to move on to the next. If there is no more file, the software will return an error. Thats why we created the following code:

  ' find the first file
  if objFtp.LastError <> 0 then
    response.write("Connection failed..<br>")
  else
    set objFiles = objFtp.FindFirstFile()
  end if
  
  ' list the files
  if objFtp.LastError <> 0 then
    response.write("<tr><td>Sorry.. Failed to list the files..</td></tr>")
  else
    getfiles
  end if  


sub getfiles

  'show the contents in a table
  response.write ("Files in directory " & request("directory") & " :")  
  response.write ("<table>")
  
  'the software will return an error if there is no more file to display, so:
  do while objFtp.LastError = 0
     response.write("<tr>")
     response.write("<td>")
     
     'display folders
     if objFiles.IsDirectory = 0 then
       response.write "File:"
     else
       response.write "Directory:"
     end if
     
     response.write("</td>")
     response.write("<td class=files>")                    
     
     'display the names
     response.write(objFiles.name)
     
     response.write("</td>")
     response.write("</tr>")
     
     'find the next file; continue the loop
     set objFiles = objFtp.FindNextFile 
  loop

  response.write "</table>"
  
end sub

Appendix: Full source code

<object runat="server" progid="AxNetwork.FtpServer" id="objFtpServer"> </object>

<%
' HTML-CSS layout includes below, no code there!
%>
<!-- #include file="css/Header.html" -->
<!-- #include file="css/Menu.html" -->

<%
  ' A license key is required to unlock this component after the trial period has expired.
  ' Call 'Activate' with a valid license key as its first parameter. Second parameter determines whether to save the license key permanently 
  ' to the registry (True, so you need to call Activate only once), or not to store the key permanently (False, so you need to call Activate
  ' every time the component is created). For details, see manual, chapter "Product Activation".
  '
  ' objFtpServer.Activate "XXXXX-XXXXX-XXXXX", False    
%>

<%
  Dim numLastError, strLastError, strResponse

  Set fso = CreateObject("Scripting.FileSystemObject")
  strLogfile = fso.GetSpecialFolder(2) & "\Ftp.log"
  
  strLastError = ""
  strResponse  = ""

   If( Request( "btnSubmit" ) <> "" ) Then
   
    objFtpServer.LogFile       = Request( "txtLogFile" )    ' Optional: set LogFile property for troubleshooting purposes
    If( Request( "txtPasv" ) <> "" ) Then
      objFtpServer.PassiveMode  = True
    Else
      objFtpServer.PassiveMode  = False
    End If
    objFtpServer.HostPort        = Request( "txtPort" )
    objFtpServer.Connect Request( "txtHost" ), Request( "txtFtpAccount" ), Request( "CTL_FTPPASSWORD" )
    numLastError                 =  objFtpServer.LastError
    If( numLastError = 0 ) Then
      strResponse              = objFtpServer.LastResponse
      If( Request( "chbDir" ) <> "" ) Then    
        objFtpServer.ChangeDir Request( "chbDir" )
         numLastError        = objFtpServer.LastError
         If( numLastError = 0 ) Then
          strResponse      = strResponse & vbCrLf & objFtpServer.LastResponse
         End If 
      End If
      objFtpServer.Disconnect
    End If

    strLastError =  numLastError & ": " & objFtpServer.GetErrorDescription( numLastError )  
   
  End If
%>

<div>
  <h1>ActiveXperts Network Component ASP Sample - FTP</h1>
  <form action="ftp.asp" method="post">
    <h2>Component:</h2>
    <h3>Module [<% = objFtpServer.Module %>]; Build [<% = objFtpServer.Build %>]</h3>
    
    <!-- FTP Server -->
    <label for="FTPServer">FTP Server:</label>
    <p>
      <input type="text" name="txtHost" value="<% If Request( "txtHost" ) = "" Then%>ftp.activexperts-lab.com<% Else %><% = Request( "txtHost" ) %><% End If %>">
      <input style="width: 30px" type="text" name="txtPort" value="<% If Request( "txtPort" ) = "" Then%>21<% Else %><% = Request( "txtPort" ) %><% End If %>">
         
      <% If( Request( "txtPasv" ) = "pasv" Or Request( "txtPasv" ) = "" ) Then %>
        <input type="checkbox" name="txtPasv" value="pasv" checked>
      <% Else %>
        <input type="checkbox" name="txtPasv" value="actv">
      <% End If %>
      PASV
    </p>
    
    <!-- FTP Account -->
    <label for="FTPAccount">FTP Account:</label>
    <p>
      <input type="text" name="txtFtpAccount" value="<% If Request( "txtFtpAccount" ) = "" Then %>anonymous<% Else %><% = Request( "txtFtpAccount" ) %><% End If %>">
    </p>
    
    <!-- FTP Password -->
    <label for="FTPPassword">FTP Password:</label>
    <p>
      <input type="password" name="CTL_FTPPASSWORD" value="<% If Request( "CTL_FTPPASSWORD" ) = "" Then %>me@myself.dom<% Else %><% = Request( "CTL_FTPPASSWORD" ) %><% End If %>">
    </p>

    <!-- Change Directory -->
    <label for="FTPPassword">Change directory:</label>
    <p>
      <input type="text" name="chbDir" value="<% If Request( "chbDir" ) = "" Then %>/Samples/network-component<% Else %><% = Request( "chbDir" ) %><% End If %>">
    </p>
    
    <!-- Submit Button -->
    <div></div>
    <p>
      <input type="submit" value="Connect" name="btnSubmit">
    </p>

    <!-- Logfile -->
    <label for="Logfile">Logfile:</label>
    <p>
      <input type="text" id="LogFile" name="txtLogFile" value="<% = strLogfile %>" />
    </p>
    
    <!-- Result -->
    <label for="Result">Result:</label>
    <p>
      <input type="text" id="Result" name="txtResult" style="font-weight: bold;" value="<% = strLastError %>" />
    </p>
    
    <!-- Response -->
    <label for="Result">Response:</label>
    <p>
      <textarea name="CTL_RESPONSE" rows="10" cols="60"><% = strResponse %></textarea></td>
    </p> 
  </form>
  <p>
    This sample is based on ActiveXperts Network Component, an <a href="https://www.activexperts.com" target="_blank">ActiveXperts Software</a> product.<br />
    <a href="Default.asp">Back to main page</a>
  </p>
</div><!-- /container -->
<%
' HTML-CSS layout include below, no code there!
%>
<!-- #include file="css/Footer.html" -->

You can download the complete samples here. There are many other working Network Component scripts on our site and shipped with the product.