<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="setup()">
<mx:Script>
<![CDATA[
import co.uk.mikestead.net.URLRequestBuilder;
import co.uk.mikestead.net.URLFileVariable;
import mx.controls.Alert;
import mx.collections.ArrayCollection;
import flash.net.URLVariables;
import flash.events.*;
/**
* Replace this with the location you've placed form_handler.php.
* This should be under the same domain as the published swf or you'll need
* a cross domain policy file.
*/
public const WEB_SERVICE_URL:String = "http://localhost/multipartform/form_handler.php";
private var filePicker:FileReferenceList;
private var fileLoadCount:uint;
private var variables:URLVariables;
/**
* Create file picker and set all fields to default values.
*/
private function setup():void
{
filePicker = new FileReferenceList()
filePicker.addEventListener(Event.SELECT, onFilesPicked);
resetFields();
}
/**
* Event handler for file picked event. This gives us the array of files
* selected by the user.
*
* As soon as the files are selected we load them into memory. We do this
* right away as this action (as of player 10) needs to be in response to
* a user interaction.
*
* If we chose to load these when the user clicked "submit" then they would
* have to click submit twice because, as of player 10, you can not send a URLRequest
* containing mulitipart data unless it is also triggered by a user interaction event.
*/
private function onFilesPicked(event:Event):void
{
var fileList:Array = filePicker.fileList;
filesGrid.dataProvider = new ArrayCollection(fileList);
loadFileVariables();
submitBtn.enabled = false;
}
/**
* Load the files the user has selected
*/
private function loadFileVariables():void
{
fileLoadCount = 0;
variables = new URLVariables();
var fileList:Array = filePicker.fileList;
for each(var file:FileReference in fileList)
{
file.addEventListener(Event.COMPLETE, onFileInMemory)
file.addEventListener(IOErrorEvent.IO_ERROR, onError);
file.load();
}
}
/**
* Event handler called each time an file loads into memory.
*
* Here we wrap the file data in a URLFileVariable and add it to our URLVariables.
*/
private function onFileInMemory(event:Event):void
{
var file:FileReference = FileReference(event.target);
variables["file" + fileLoadCount] = new URLFileVariable(file.data, file.name);
if (++fileLoadCount >= filePicker.fileList.length)
submitBtn.enabled = true;
}
/**
* Called when user clicks the submit button.
*
* Adds string variables to URLVaraibles instance and then asks URLRequestBuilder
* to create a URLRequest with these. Then create a URLLoader and send the data
* off to the server.
*/
private function submit():void
{
variables.userName = userName.text;
variables.userAge = userAge.text;
var request:URLRequest = new URLRequestBuilder(variables).build();
request.url = WEB_SERVICE_URL;
var loader:URLLoader = new URLLoader();
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
loader.addEventListener(Event.COMPLETE, onFormSubmitSuccess)
try
{
loader.load(request);
}
catch (error:Error)
{
trace("Unable to dispatch load request: " + error);
}
}
/**
* Event handler called when server returns with a valid response.
*/
private function onFormSubmitSuccess(event:Event):void
{
var loader:URLLoader = URLLoader(event.target);
Alert.show("Form was submitted successfully\n\n" + loader.data);
resetFields();
}
/**
* Event handler to deal with any issues during file load/upload.
*/
private function onError(event:Event):void
{
Alert.show(event.toString());
submitBtn.enabled = true;
}
/**
* Reset all fields to their defaults.
*/
private function resetFields():void
{
fileLoadCount = 0;
variables = new URLVariables();
filesGrid.dataProvider = null;
submitBtn.enabled = true;
}
]]>
</mx:Script>
<mx:VBox width="100%" height="100%" verticalAlign="middle" horizontalAlign="center">
<mx:FormHeading label="Multipart Form Example" color="#FFFFFF"/>
<mx:Form id="mainForm" width="80%">
<mx:FormItem label="Name:" width="100%">
<mx:TextInput id="userName" text="John Roberts" width="100%"/>
</mx:FormItem>
<mx:FormItem label="Age:" width="100%">
<mx:TextInput id="userAge" text="26" width="100%"/>
</mx:FormItem>
<mx:FormItem label="Files:" width="100%">
<mx:HBox width="100%">
<mx:DataGrid id="filesGrid" width="100%">
<mx:columns>
<mx:DataGridColumn headerText="Name" dataField="name" />
<mx:DataGridColumn headerText="Size" dataField="size"/>
</mx:columns>
</mx:DataGrid>
<mx:Button label="Browse" click="filePicker.browse();"/>
</mx:HBox>
</mx:FormItem>
</mx:Form>
<mx:Button id="submitBtn" x="99" y="61" label="Submit" click="submit()"/>
</mx:VBox>
</mx:Application>