Recipe 23.7.
Uploading Files
Problem
You want
to allow users to upload files.
Solution
Use the upload( ) method of the
FileReference object.
Discussion
The upload( ) method of a
FileReference object allows you to upload a file to a server
by using a server-side script that is configured to accept uploads
using HTTP(S). At a minimum, the upload( ) method requires
one parameter as a URLRequest object specifying the URL of
the script to which you want to send the file data:
var urlRequest:URLRequest = new URLRequest("uploadScript.cgi");
fileReference.upload(urlRequest);
All uploads use POST with a
Content-Type of multipart/form-data. By default,
the Content-Disposition is set to Filedata. The
script needs to know the Content-Disposition value so it
can read the file data. If the script needs to use a
Content-Disposition other than the default, you can
specify a value as the second, optional parameter you pass to the
upload( ) method, as follows:
fileReference.upload(urlRequest, "UploadFile");
You can only upload a file if the user has
selected the file using browse( ). If the user selects a
file using a FileReference object's browse( ) method,
then you can call upload( ) after the select event
occurs. If the user selects files using FileReferenceList,
then you must call the upload( ) method for each file in the
object's fileList property. The fileList property
of a FileReferenceList object is an array of
FileReference objects corresponding to each of the files
selected by the user.
The upload( ) method can throw errors.
The possible errors thrown by upload( ) are identical to
those thrown by download( ). Additionally, like download(
), the upload( ) method can dispatch security error
events and IO error events. See Recipe
23.1 for more details on handling these errors and error
events.
See Also
Recipe
23.1
|