Search Flex Samples

flex Loading data

SWF files can load data from servers into ActionScript, and send data from ActionScript to servers. Loading data is a different kind of operation from loading media, because the loaded information appears directly in ActionScript, rather than being displayed as media. Generally, SWF files may load data from their own domains. However, they usually require cross-domain policy files in order to load data from other domains.


Using URLLoader and URLStream

You can load data, such as an XML file or a text file. The load() methods of the URLLoader and URLStream classes are governed by cross-domain policy file permissions.

If you use the load() method to load content from a domain other than that of the SWF file that is calling the method, Flash Player checks for a cross-domain policy file on the server of the loaded assets. If there is a cross-domain policy file, and it grants access to the domain of the loading SWF file, you can load the data.

Connecting to sockets

Cross-domain access to socket and XML socket connections is disabled by default. Also disabled by default is access to socket connections in the same domain as the SWF file on ports lower than 1024. You can permit access to these ports by serving a cross-domain policy file from any of the following locations:

  • The same port as the main socket connection
  • A different port
  • An HTTP server on port 80 in the same domain as the socket server

If you serve the cross-domain policy file from same port as the main socket connection, or in a different port, you enumerate permitted ports by using a to-ports attribute in the cross-domain policy file, as the following example shows:

To retrieve a socket policy file from the same port as a main socket connection, simply call the Socket.connect() or XMLSocket.connect() method, and, if the specified domain is not the same as the domain of the calling SWF file, Flash Player automatically attempts to retrieve a policy file from the same port as the main connection you are attempting. To retrieve a socket policy file from a different port on the same server as your main connection, call the Security.loadPolicyFile() method with the special "xmlsocket" syntax, as in the following:


Call the Security.loadPolicyFile() method before calling the Socket.connect() or XMLSocket.connect() method. Flash Player then waits until it has fulfilled your policy file request before deciding whether to allow your main connection.

If you are implementing a socket server and you need to provide a socket policy file, decide whether you will provide the policy file using the same port that accepts main connections, or using a different port. In either case, your server must wait for the first transmission from your client before deciding whether to send a policy file or set up a main connection. When Flash Player requests a policy file, it always transmits the following string as soon as a connection is established:

Once the server receives this string, it can transmit the policy file. Do not expect to reuse the same connection for both a policy file request and a main connection; you should close the connection after transmitting the policy file. If you do not, Flash Player closes the policy file connection before reconnecting to set up the main connection.

For more information, see Socket policy files.

Sending data

Data sending occurs when ActionScript code from a SWF file sends data to a server or resource. Sending data is always permitted for network domain SWF files. A local SWF file can send data to network addresses only if it is in the local-trusted or local-with-networking sandbox. For more information, see Local sandboxes.

You can use the function to send data to a URL. Other methods also send requests to URLs. These include loading methods, such as Loader.load() and Sound.load(), and data-loading methods, such as URLLoader.load() and URLStream.load().

Uploading and downloading files

The FileReference.upload() method starts the upload of a file selected by a user to a remote server. You must call the FileReference.browse() or FileReferenceList.browse() method before calling the FileReference.upload() method.

Calling the method opens a dialog box in which the user can download a file from a remote server.

Note: If your server requires user authentication, only SWF files running in a browser--that is, using the browser plug-in or ActiveX control--can provide a dialog box to prompt the user for a user name and password for authentication, and only for downloads. Flash Player does not allow uploads to a server that requires user authentication.

Uploads and downloads are not allowed if the calling SWF file is in the local-with-filesystem sandbox.

By default, a SWF file may not initiate an upload to, or a download from, a server other than its own. A SWF file may upload to, or download from, a different server if that server provides a cross-domain policy file that grants permission to the domain of the invoking SWF file.


Related Flex Samples

Learn Flex: Flex Samples | Flex Video Tutorials Flex Examples