| Scripting File Uploads |
While it is possible to use and customize AppletFile using only applet parameters, a scripting interface is also available for advanced processing using client-side JavaScript.
Initialization
Polling for File List changes
Polling for upload completion
It is recommended that you use applet parameters to customize AppletFile whenever possible. The scripting interface is intended only for developers that have advanced expertise in client-side JavaScript programming.
Before calling any of the scripting methods, you must first wait until the File Upload Applet has been loaded and has finished initializing. This can be accomplished by calling a polling function from your document's onLoad event. For example:
<HTML><HEAD>
<SCRIPT language="JavaScript">
<!--
function waitForInit()
{
var applet = document.FileUpload;
if (applet.hasInitialized())
{
// Okay to execute other methods
:
:
}
else // Keep polling until applet initializes
window.setTimeout('waitForInit()',200);
}
//-->
</SCRIPT></HEAD>
<BODY onLoad="waitForInit();">
:
:
Once the hasInitialized method returns true, it is safe to call the other scripting methods. Attempts to call other scripting methods prior to this will be ignored by the applet.
Changes to the File List that can occur through the File List 'Add' and 'Remove' buttons as well as calls to the selectAdd and selectRemove methods happen asynchronously. If you need to perform your own custom validation and processing when these changes occur, you will need to poll for these changes using the hasChanged method. For example:
function checkForChanges()
{
if (document.FileUpload.hasChanged())
{
// Perform custom validation and processing
:
:
}
window.setTimeout('checkForChanges()',200);
}
Once a change to the File List is reported, you can call a variety of methods include getFile, getFileCount, getFileDate, and getFileSize to perform any preprocessing to the files prior to the upload. The data returned by these methods can then be used to validate if the file should be uploaded or simply saved for tracking purposes. You can also use this data to implement your own custom File List interface as demonstrated by the Custom Upload sample application.
When the submit method is called it runs asychronously. If you wish to perform any processing after the upload, you will need to poll for completion by calling getUploadProgress until it returns 100. You can accomplish this by adding the following function to your page and calling it right after you call the upload method.
function waitForCompletion()
{
var percent = document.FileUpload.getUploadProgress();
if (percent > 0) window.status = percent + '% complete';
if (percent == 100)
{
alert("Upload complete!");
window.status = '';
}
else if (percent == -2)
window.status = "Upload canceled!";
else
window.setTimeout('waitForCompletion()',100);
}