User Guide
Working with file upload and download 401
}
function completeHandler(event:Event):void
{
trace("uploaded");
}
The previous example creates a new URLVariables object that you pass to the remote server-
side script. In previous versions of ActionScript, you could pass variables to the server upload
script by passing values in the query string. ActionScript 3.0 allows you to pass variables to the
remote script using a URLRequest object, which allows you to pass data using either the
POST
or
GET method; this, in turn, makes passing larger sets of data easier and cleaner. In order to
specify whether the variables are passed using the
GET or POST request method, you can set the
URLRequest.method property to either URLRequestMethod.GET or
URLRequestMethod.POST, respectively.
ActionScript 3.0 also lets you override the default
Filedata upload file field name by
providing a second parameter to the
upload() method, as demonstrated in the previous
example (which replaced the default value
Filedata with Custom1).
By default, Flash Player will not attempt to send a test upload, although you can override this
by passing a value of
true as the third parameter to the upload() method. The purpose of
the test upload is to check whether the actual file upload will be successful and that server
authentication, if required, will succeed.
Downloading files from a server
You can let users download files from a server using the FileReference.download()
method, which takes two parameters:
request and defaultFileName. The first parameter is
the URLRequest object that contains the URL of the file to download. The second parameter
is optional—it lets you specify a default filename that appears in the download file dialog box.
If you omit the second parameter,
defaultFileName, the filename from the specified URL is
used.
The following code downloads a file named index.xml from the same directory as the SWF
document:
var request:URLRequest = new URLRequest("index.xml");
var fileRef:FileReference = new FileReference();
fileRef.download(request);
NOTE
A test upload occurs only on Windows-based Flash Players at this time.