Validating the File List

The AppletFile Upload Applet supports several parameters and methods that allow you to validate the files being uploaded. The topics below describe the different validation techniques that can be used:

Controlling the number of files
Controlling the size of files
Advanced validation


Controlling the number of files

The maxFiles and minFiles parameters can be used to require a specific number or range of files that must be selected for upload. For example, the following would require that the user select at least 1 file, but no more than 2:

<FORM ACTION="upload.asp" 
      onSubmit="return document.FileUpload.submit(document,this,true)">
</FORM>
<APPLET CODE=FileUpload.class ARCHIVE=FileUpload.jar NAME=FileUpload 
        CODEBASE="/AppletFile/classes" WIDTH=420 HEIGHT=180 MAYSCRIPT>
<PARAM NAME="cabbase" VALUE="/AppletFile/classes/FileUploadIE3.cab">
<PARAM NAME="cabinets" VALUE="/AppletFile/classes/FileUpload.cab">
<PARAM NAME="minFiles" VALUE="1">
<PARAM NAME="maxFiles" VALUE="2">
</APPLET>

Controlling the size of files

The size of files that can be uploaded are controlled with the errorLimit and warningLimit parameters. In the example below, the user will be given a warning if any file is selected that is larger than 200,000 bytes and an error will occur if any file is selected that is larger than 1,000,000 bytes.

<FORM ACTION="upload.asp" 
      onSubmit="return document.FileUpload.submit(document,this,true)">
</FORM>
<APPLET CODE=FileUpload.class ARCHIVE=FileUpload.jar NAME=FileUpload 
        CODEBASE="/AppletFile/classes" WIDTH=420 HEIGHT=180 MAYSCRIPT>
<PARAM NAME="cabbase" VALUE="/AppletFile/classes/FileUploadIE3.cab">
<PARAM NAME="cabinets" VALUE="/AppletFile/classes/FileUpload.cab">
<PARAM NAME="errorLimit" VALUE="1000000">
<PARAM NAME="warningLimit" VALUE="200000">
</APPLET>

Advanced validation

In addition to the validation parameters discussed above, there are several scripting methods that can be used to access the contents of the file list. Using client-side JavaScript, you can use these methods to implement your own custom validation logic.

Use this method: If you need to:                                                
getFile retrieve and validate the file name
getFileCount validate the number of files
getFileDate compare and validate file modification dates
getFileSize validate the size of files
removeFile remove an invalid file

The first step is to write your validation routine. In the following example, we will validate that the user has selected one spreadsheet and one word processing document:

<SCRIPT language="JavaScript">
<!--
function MyValidation()
{
    var list = document.FileUpload;

    // First make sure the applet has initialized
    if (!list.hasInitialized())
        return false; // silently fail 

    // Next make sure that there are two files
    if (list.getFileCount() != 2)
    {
        alert('Please select 2 files, not ' + list.getFileCount());
        return false;
    }
    
    // Finally make sure they are the right types
    var file1 = new String(list.getFile(1));
    var file2 = new String(list.getFile(2));
    var ext1 = file1.substring(file1.lastIndexOf('.')+1,file1.length);
    var ext2 = file2.substring(file2.lastIndexOf('.')+1,file2.length);
    if (ext1 != '.xls' && ext2 != '.xls')
    {
        alert('You forgot to select a spreadsheet file');
        return false;
    }
    if (ext1 != '.doc' && ext2 != '.doc')
    {
        alert('You forgot to select a word processing file');
        return false;
    }

    // File list is valid
    return true;
}
//-->
</SCRIPT>
! Some JavaScript implementations return String results as JavaScript String objects while other JavaScript implementations return them as Java String objects. To avoid these differences, the above example always creates a new JavaScript String using the return value from each call to list.getFile.

The next step is to add the validation to your upload form by modifying the onSubmit attribute that calls the submit method. Simply replace true with a call to your validation function.

<FORM ACTION="upload.asp" 
      onSubmit="return document.FileUpload.submit(document,this,MyValidation())">
   :
   :
</FORM>