Uploading a Directory


JFile allows the user to upload one or more individual files, or a whole directory. JFile's addDirectory method adds the contents of a directory, excluding sub-directories, to the upload. addDirectoryRecursive uploads a directory with its subdirectories, maintaining the original directory structure. When uploading a directory, use SoftArtisans FileUp's SaveRecursive method in your response script, as demonstrated in Exercise 4.

To upload a directory recursively, check Recursive. Using the system's native file selection dialog, you cannot browse to a folder and click "Open" to add it to the file/folder list. Instead, enter the directory path and name in the File/Dir input field. To allow the user to browse to a folder and select it, set JFile's UseNativeFileSelection parameter to 0. When UseNativeFileSelection is disabled, JFile will display a platform-neutral file selector when the user clicks Add Files/Directory.

JavaScript methods are case-sensitive.
Recursive Directory Upload

Exercise: Uploading a Directory

  1. If you created the physical and virtual JFileScripts directories in a previous lesson, go to Step 4. If you did not create the directories, go to Step 2.

  2. Create a directory called JFileScripts.

  3. Create a virtual directory called JFileScripts and map it to the physical JFileScripts you created in Step 2:

    1. Open the Internet Information Services (IIS) explorer.
    2. Right-click Default Web Site.
    3. Select New -> Virtual Directory.
    4. For Alias, enter JFileScripts.
    5. Browse to the JFileScripts directory you created in step 1.
    6. Check "Read" and "Run scripts."
    7. Click "Finish."

  4. Copy filetransfer.jar (for Netscape) and/or filetransfer.cab (for Internet Explorer) to the directory JFileScripts. These files are in JFile's installation directory (by default, C:\Program Files\SoftArtisans\JFile).

  5. Create a server side upload script:

    1. Open formresp.asp to edit it. Formresp.asp is a server-side upload processing script that you will find in JFile's Samples\Upload\Simple directory (installed by default at C:\Program Files\SoftArtisans\JFile). Formresp.asp uses SoftArtisans FileUp to process the upload.
    2. Replace the line upl.Form(item).SaveAs filename with upl.SaveRecursive().
    3. Save formresp.asp as RecursJFileResp.asp in the directory JFileScripts.

  6. Create an HTML script containing the following lines. Replace "X:\\FolderName\\SubFolderName" with a valid directory path. Name the script RecursJFile.htm and save it in your JFileScripts directory.
    <!-- The script's onload event calls the function myTransferProgress(), defined below. -->
    <BODY language="javascript" onload="myTransferProgress()">
    
    <APPLET codebase="/JFileScripts" code="softartisans.filetransfer.UploadClient.class" 
    height="0" width="0" mayscript archive="filetransfer.jar" name="fileupload">
    	
    	<!-- The Cabbase parameter specifies the cab file containing the applet. -->
    	<PARAM name="cabbase" value="filetransfer.cab">
    
    	<!-- The PostURL parameter specifies the URL to which the file will be posted. -->
    	<PARAM name="PostURL" value="http://localhost/jfilescripts/RecursJFileResp.asp">
    			
    </APPLET>
    
    <SCRIPT language="Javascript">
    
    // JFile will set the variable SAJFileAppletLoaded to True when the applet is loaded. 
    var SAJFileAppletLoaded = False;
    
    // The function myTransferProgress() adds a directory to the applet's upload list.
    function myTransferProgress()
    	{
    	if(SAJFileAppletLoaded)
    		{
    		// addDirectoryRecursive adds the specified directory to the list of files and folders to 
    		// upload.  Enter a valid directory and path.
    		document.fileupload.addDirectoryRecursive("X:\\FolderName\\SubFolderName")
    		}
    	else
    		alert("Applet is not loaded.");
    	}
    </SCRIPT>
    </BODY>

Understanding the Script


Copyright © 2000-2003 SoftArtisans, Inc. All rights reserved.