Transferring files to an IBM mainframe using FTP

In this example, an IBM 3270 session is in a separate browser window, and HTML form elements are used to provide controls for performing FTP file transfers between the desktop computer and the mainframe. The Transfer type radio buttons allow users to choose the method for sending or receiving files. Because the user is given access only to the basic menus, the administrator may want to preconfigure transfer options in the Connection Setup dialog box, then save those settings to a configuration file.

To use this example, you must change the hostURL parameter in the <applet> tag below to a host appropriate for your network.

<html>
<head>
<title>FTP API Example</title>
<script language="JavaScript">
<!--

    var api = null;

    /*
        Copy relevant symbolic constants from file server_root/webapps/rweb/admin/en/html/advanced/api_constants/api_all_constants.js
    */
    var FTP_ASCII      = 0;
    var FTP_BINARY     = 1;
    var FTP_SMART      = 2;
    
    /*
        Returns the JSAPI for the terminal session.
    */
    function getJSAPI()
    {
       if ( api == null )
       {
         if ( document.ibm3270 != null )
         {
            api = document.ibm3270.getAPI( "JSAPI", "IBM3270" );
         }
       }  
       return api;
    }

    /*
        This function performs the login to the FTP server. It first checks for
        the presence of a hostname, username, and password, and displays an alert
        message if any of these entries is missing. An alert message displays
        the status of the login attempt.
    */
    function doftpLogin()
    {
        var hostname = document.ftp.hostname.value;
        var username = document.ftp.username.value;
        var password = document.ftp.password.value;
        if ( hostname == "" || username == "" || password == "" )
        {
            alert( "You must enter a hostname, username, and " +
                   "password to login. One of these entries is missing." );
            return;
        }
        var result = getJSAPI().ftpLogin(hostname, username, password, null, true);
        if ( result )
            alert( "FTP login was successful." );
        else
            alert( "FTP login failed." );
    }

    /*
        This function disconnects from the FTP server. An alert message
        displays the status of the disconnect attempt.    
     */
    function doftpDisconnect()
    {
        var result = getJSAPI().ftpDisconnect();
        if ( result ) 
            alert( "FTP disconnect was successful." );
        else
            alert( "FTP disconnect failed." );
    }

    /*
        This function sends a file to the FTP server. It does not perform any 
        validity checking of the file names other than ensuring that there
        are entries in the local and remote file name fields. The transfer 
        is performed as either ASCII, binary, or smart transfer, depending 
        on the state of the Transfer type radio buttons, and server directories 
        are created automatically if the "Automatically create directory" check
        box is selected. An alert message displays the status of the transfer.
     */
    function doftpSendFile()
    {
        var remotefile = document.ftp.remoteFile.value;
        var localfile = document.ftp.localFile.value;
        if ( !validateXferInputs(remotefile, localfile) )
            return;
        var makedir = document.ftp.makeDirectory.checked ? true : false;
        var xfertype = FTP_ASCII;   // default to ASCII
        if ( document.ftp.AsciiBinary[1].checked )
            xfertype = FTP_BINARY;   // binary
        else if ( document.ftp.AsciiBinary[2].checked )
            xfertype = FTP_SMART;   // smart transfer
        var result = getJSAPI().ftpSendFile(remotefile,localfile,makedir,xfertype);
        if ( result ) 
            alert( "FTP transfer to host was successful." );
        else
            alert( "FTP transfer to host failed." );
    }

    /*
        This function transfers a file from the FTP server to the local machine.
        No validity checking of file names is performed other than ensuring that
        there are entries in the local and remote file name fields. The transfer 
        is performed as either ASCII, Binary or smart transfer, depending on the
        state of the Transfer type radio buttons. An alert message displays the
        status of the transfer.
     */
    function doftpReceiveFile()
    {
        var remotefile = document.ftp.remoteFile.value;
        var localfile = document.ftp.localFile.value;
        if ( !validateXferInputs(remotefile, localfile) )
            return;
        var xfertype = FTP_ASCII;   // default to ASCII
        if ( document.ftp.AsciiBinary[1].checked )
            xfertype = FTP_BINARY;   // binary
        else if ( document.ftp.AsciiBinary[2].checked )
            xfertype = FTP_SMART;   // smart transfer
        var result = getJSAPI().ftpReceiveFile(remotefile,localfile,xfertype);
        if ( result ) 
            alert( "FTP transfer from host was successful." );
        else
            alert( "FTP transfer from host failed." );
    }

    /*
        This function validates the entries in the Local File and Remote File
        text fields. There must be entries in both of these fields before you
        can transfer files.
    */
    function validateXferInputs(remotefile, localfile)
    {
        if ( remotefile == "" || localfile == "" )
        {
            alert( "You must specify both a remote file name and a local file " +
                   "name before starting the transfer." );
            return false;
        }
        return true;
    }

    /*
        This function gets the last response from the FTP server. If one of the
        other functions fails to complete successfully, the last server response 
        contains the specific message.
     */
    function doftpGetLastServerResponse()
    {
        var result = getJSAPI().ftpGetLastServerResponse();
        alert( "Last response from FTP server:\n" + result );
    }

//-->
</script>
</head>
<body>
<h1>Reflection for the Web: FTP API Example</h1>
<hr>
<!-- 
    Use form items to collect the information needed to perform the FTP API
    functions.
-->
<form name="ftp">
<table>
    <tr>
        <td><b>Connection:</b></td>
        <td> </td>
    </tr>
    <tr>
        <td>Host name:</td>
        <td><input type="text" value="" name="hostname"></td>
    </tr>
    <tr>
        <td>User name:</td>
        <td><input type="text" value="" name="username"></td>
    </tr>
    <tr>
        <td>Password:</td>
        <td><input type="password" value="" name="password"></td>
    </tr>
</table>
<p><input type="button" name="ftplogin" value="Log In" onClick="doftpLogin()">
<input type="button" name="ftpdisconnect" value="Disconnect" onClick="doftpDisconnect()">
</p>
<table>
    <tr>
        <td><b>File to transfer:</b></td>
        <td> </td>
    </tr>
    <tr>
        <td>Local file:</td>
        <td><input type="text" value="" name="localFile"></td>
    </tr>
    <tr>
        <td>Remote file:</td>
        <td><input type="text" value="" name="remoteFile"></td>
    </tr>
    <tr>        
        <td> </td>
        <td><input type="checkbox" value="makeDirectory" 
            name="makeDirectory">Automatically create directory</td>
    </tr>
    <tr>
        <td>Transfer type:</td>
        <td><input type="radio" value="ascii" name="AsciiBinary" checked>ASCII 
            <input type="radio" value="binary" name="AsciiBinary">Binary 
            <input type="radio" value="smart" name="AsciiBinary">Smart</td> 
    </tr>
</table>
<p>
<input type="button" name="ftpSendFile" value="Send File" onClick="doftpSendFile()">
<input type="button" name="ftpReceiveFile" value="Receive File" onClick="doftpReceiveFile()">
</p>
<p>
<b>Last server response:</b><br>
<input type="button" name="ftpGetLastServerResponse" value="Display" onClick="doftpGetLastServerResponse()">
</p>
</form>
<p>
<!--
   Create a basic IBM 3270 applet in a separate frame, with Advanced user
   menus. To establish a connection, you need to change the hostURL
   parameter to one appropriate for your network.
-->   
<applet mayscript name="ibm3270" codebase="./ex" 
    code="com.wrq.rweb.Launcher.class" 
    width=0 height=0 archive="Launcher.jar">
    <param name="frame" value="true">
    <param name="menuType" value="advanced">
    <param name="hostURL" value="tn3270://mymainframe:23">
    <param name="ftpenabled" value="true">
    <param name="launcher.sessions" value="IBM3270">
    <param name="preloadJSAPI" value="true">
</applet>
</p>
</body>
</html>