134 | Chapter 4: AIR Mini-Cookbook
TIP
The implementation of the receiving server is beyond the
scope of this example. There are numerous technologies,
and tutorials for these technologies, that elegantly handle
file upload. You’re encouraged to investigate your
options.
The primary events that are useful are ProgressEvent.
PROGRESSandEvent.COMPLETE. These events handle notifying
the application of upload progress, and when an individual
upload is complete, respectively:
var file =
new air.File.documentsDirectory.resolve( "myImage.jpg" );
file.addEventListener( air.ProgressEvent.PROGRESS,
doProgress );
file.addEventListener( air.Event.COMPLETE, doComplete );
TheProgressEventcontains various properties that can help
in reflecting upload progress in the user interface. The most
notable of these properties areProgressEvent.byteLoadedand
ProgressEvent.bytesTotal, which show how much of the file
has been uploaded and the total size of the file. TheEvent.
COMPLETE is broadcast once the upload is complete.
To start the upload, you first need a valid File object that
points to a resource on disk.
Once a valid file reference is established, developers will
want to call theFile.upload( )method. TheFile.upload( )
method can take three arguments, the first of which is a
URLRequestobject that contains information about where the
file should be sent. TheURLRequestobject can also contain
additional data to be passed to the receiving server. This
additional data manifests itself as HTML form fields might
during a traditional multipart file upload:
var request = new air.URLRequest( "http://www.mydomain.
com/upload" );
file.upload( request, "image", false );