Uploading files to the server, Chapter 12, Solution 6

­
<?xml version="1.0" encoding="utf-8"?>
<!--
////////////////////////////////////////////////////////////////////////////////
// Flex Solutions: Essential Techniques for Flex 2 and Flex 3 Developers
// Author: Marco Casario 
// Editor: FriendsOfED www.friendsofed.com
// All Rights Reserved.
//
// Chapter 12: More Flex framework libraries and utilities
// 
// Solution 12-6: Uploading files to the server
//
// 
// @author      Marco Casario
// @date        26 November 2007
// @version     1.0
// @site        flexsolutions.comtaste.com
//
////////////////////////////////////////////////////////////////////////////////
-->
 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
	layout="vertical" 
	horizontalAlign="left"
	xmlns:net="flash.net.*"
	creationComplete="onCreationComplete()">
 
 
	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;
 
			[Bindable]
			private var fileRef:FileReference;
			[Bindable]
			private var fileSelected:Boolean = false;
			[Bindable]
			private var status:String = "Select file";
 
			private function onCreationComplete():void
			{
				fileRef = new FileReference();
				fileRef.addEventListener(Event.SELECT, onFileSelected);
				fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, onUploadComplete);
				fileRef.addEventListener(ProgressEvent.PROGRESS, onUploadProgress);
				fileRef.addEventListener(IOErrorEvent.IO_ERROR, onUploadError);
				fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onUploadError);
			}
 
 
			/**
			 * Called by the button, calls file browser window.
			 */
			private function selectFile(event:MouseEvent):void
			{
				fileRef.browse();
			}
 
			/**
			 * Called by the button, starts the upload.
			 */
			private function uploadFile(event:MouseEvent):void
			{
				if (!fileSelected || (urlInput.text.length == 0)) {
				Alert.show("Insert a URL and select a file.");
				return;
			}
 
			fileRef.upload(new URLRequest(urlInput.text));
			}
 
			/**
			 * Called when a file is selected in the file chooser window.
			 */
			private function onFileSelected(event:Event):void
			{
				fileSelected = true;
				fileInput.text = fileRef.name;
				status = "Ready";
			}
 
			/**
			 * Called on upload complete.
			 */
			private function onUploadComplete(event:DataEvent):void
			{
				status = "Complete";
			}
 
			/**
			 * Upload progress.
			 */
			private function onUploadProgress(event:ProgressEvent):void
			{
				status = ((event.bytesLoaded * 100) / event.bytesTotal).toString();
			}
 
			/**
			 * Upload error.
			 */
			private function onUploadError(event:Event):void
			{
				if (event is IOErrorEvent)
				Alert.show((event as IOErrorEvent).text.toString());
				else if (event is SecurityErrorEvent)
				Alert.show((event as SecurityErrorEvent).text.toString());
			}
		]]>
	</mx:Script>
 
	<mx:Form>
		<mx:FormItem label="Remote URL">
			<mx:TextInput id="urlInput" width="200" />
		</mx:FormItem>
		<mx:FormItem label="File name">
			<mx:TextInput id="fileInput" editable="false" width="200" />
		</mx:FormItem>
		<mx:FormItem label="Status">
			<mx:Text text="{status}" />
		</mx:FormItem>
 
		<mx:ControlBar>
			<mx:Button id="selectBut" label="Select file" click="selectFile(event)" />
			<mx:Button id="uploadBut" label="Upload file" click="uploadFile(event)" enabled="{fileSelected}" />
		</mx:ControlBar>
 
	</mx:Form>
</mx:Application>