OpenLaszlo SWF10/Flash Download with a fileReference

To download a file with script within a flash movie, you can use a FileReference.

Two points needed attention:
1. The download-event must be explicit fired, as sample by a onclick event.
2. The security requirements by the adobe flash player must be met. This mean that if the downloading file is a remote file on a separate server, the server needs a crossdomain.xml file with the needed security configuration.

Here’s a small sample for a download within a flash movie generated with OpenLaszlo:

<?xml version="1.0" encoding="UTF-8" ?>
<canvas name="URLRequestDownload" width="100%" height="100%">
		
	<switch>
		<unless property="$as3">
			<handler name="oninit">
			<![CDATA[
				Debug.error("ActionScript 3 required");
			]]>
			</handler>				
		</unless>
		<otherwise>
		
			<button text="download">
				<passthrough>
					import flash.net.FileReference;
					import flash.net.URLRequest;
				</passthrough>
					
				<handler name="onclick">
				<![CDATA[
					var fileref:FileReference = new FileReference();
					var request:URLRequest = new URLRequest("http://thomas.deuling.org/wp-content/plugins/download-monitor/download.php?id=1");
					fileref.download(request, "2011-05-04_openlaszlo_lineheight.zip");
				]]>
				</handler>
			</button>
	
		</otherwise>
	</switch>
		
</canvas>

Here’s to a sample for a crossdomain.xml that allows every access:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
  <allow-access-from domain="*" to-ports="*" secure="false"/>
  <allow-http-request-headers-from domain="*" headers="*" secure="false" />
</cross-domain-policy>

Tip:
If you’re not sure if the flash player needs a crossdomain.xml, or where he is call the crossdomain.xml, use the firebug in your firefox and look at the network-panel. There you can see every request to the crossdomain.xml. 😉

As an extentions for this, you could catch the ProgressEvent for display the download progress and many more.

You will find all the other available events and many informations here.

  1. No comments yet.

  1. No trackbacks yet.