File Upload/Download

Flex applications support file upload and download using the Flash Player flash.net.FileReference class. You can enable the user to download one file at a time, as well as select and upload one or more files at the same time.

Downloading Files

Use the download() method of a FileReference object to download a file. The download() method requires at least one parameter: a URLRequest object. The URLRequest object should point to the URL from which you want to download the file. Here’s an example that downloads a file called test.txt:

var fileReference:FileReference = new FileReference();
fileReference.download(new URLRequest("test.txt"));

As soon as the download() method is called, Flash Player will open a dialog prompting the user to accept the file. The user has complete control over whether to save the file, where to save the file, and what to name the file. However, by default, the filename field in the dialog will be filled out with the name of the file as it exists on the server. If you want to customize the default filename in the dialog, you can do so with a second parameter in the download() method. This is particularly useful when the URL from which the file is requested is a script that generates the file or proxies the request to the file. For example, if the URL from which you request the file is test.cgi, a script that outputs a text file, you likely will want to use a custom filename rather than allow it to default to test.cgi. Here’s an example:

fileReference,download(new URLRequest("test.cgi"), "test.txt");

The download() method can potentially throw errors. The two most common error types are IllegalOperationError and SecurityError. An IllegalOperationError occurs when a save dialog is already open (because only one can be open at a time). The SecurityError type occurs when the .swf is untrusted in the domain to which it is trying to make the request.

As a file downloads, you can optionally monitor progress from within the Flex application. The FileReference object downloading the file dispatches progress events as the file downloads and then a complete event once the file has downloaded.

Uploading Files

The FileReference.browse() method allows you to enable the user to browse his files and select one for upload. Optionally, you can use FileReferenceList.browse() to allow the user to select one or more files at a time.

In either case (FileReference or FileReferenceList) the browse dialog has two buttons allowing the user to close the dialog: Open and Cancel. The FileReference or FileReferenceList object dispatches different events depending on which button the user clicks. If the user clicks the Cancel button, it dispatches a cancel event (Event.CANCEL). If the user clicks the Open button, it dispatches a select event (Event.SELECT).

When the user has selected a file or files (the select event has been dispatched), you can start to upload the file or files using the upload() method. The method requires that you pass it a URLRequest object specifying the URL to which to upload the file:

fileReference.upload(new URLRequest("upload.php"));

If you’re using a FileReferenceList object, you must call the upload() method of each instance stored within the fileList property:

var request:URLRequest = new URLRequest("upload.php");
for(var i:int = 0; i < fileReferenceList.fileList.length; i++) {
    fileReferenceList.fileList[i].upload(request);
}

To upload files from Flash Player, you must have a script on the server that is capable of receiving the requests. When a file upload request is made to the server-side script, it's made in exactly the same way as a standard request from an HTML form submit request with a file field: using POST with a content type of multipart/form-data. The Content-Disposition header value is Filedata by default. If you need to customize this setting, you can specify a Content-Disposition header value using the second parameter in the upload() method:

fileReference.upload(request, "UploadFile");

Because file uploading is usually not instantaneous, it can be useful to monitor the progress of the upload. As with file downloads, the FileReference object dispatches events as a file is uploading. It will dispatch progress events as each piece of the file is uploaded, and when the file has completely uploaded, it will dispatch a complete event.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset