Using FileReference to upload files in AIR (apollo), don't forget to use request.method=Post
Posted At : June 14, 2007 4:11 PM
I was showing off Adobe AIR (formerly apollo) to a client, trying to create a simple file upload mini app. I followed the documentation, but was constantly getting the following error whenever I tried to upload.
Error #2044: Unhandled IOErrorEvent:. text=Error #2038: File I/O Error. at AIRFileUpload$iinit() at _AIRFileUpload_mx_managers_SystemManager/create() at mx.managers::SystemManager/::initializeTopLevelWindow() at mx.managers::SystemManager/::docFrameHandler()
I finally found the following technote which led me to the missing ingredient:
request.method = URLRequestMethod.POST;
Once I added that my application was working just fine.
Here is the code:
AIRFileUpload.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" height="150" width="300">
<mx:Script>
<![CDATA[
import flash.net.FileReference;
import flash.events.*;
private var fileRef:FileReference = new FileReference();Çspan style='color: #808080'ÈÇemÈ //Used to hold the file we are uploading
Ç/emÈÇ/spanÈ private var uploadURL:String = "http://127.0.0.1/projects/air/AirFileUpload/bin/upload.cfm";Çspan style='color: #808080'ÈÇemÈ // URL for server side upload script
Ç/emÈÇ/spanÈ
[Bindable]
private var statusMsg:String = "No file Selected";Çspan style='color: #808080'ÈÇemÈ // Used in status label
Ç/emÈÇ/spanÈ
/* Activate the OS file selection menu */
private function selectFile():void {
try
{
var success:Boolean = fileRef.browse();
fileRef.addEventListener(Event.SELECT, selectHandler);
}
catch (error:Error)
{
trace("Unable to browse for files.");
}
}
/* Uploads the file to the server */
private function uploadFile():void {
var request:URLRequest = new URLRequest();
request.url = uploadURL;
request.method=URLRequestMethod.POST;
try
{
fileRef.upload(request,"Filedata");
fileRef.addEventListener(Event.COMPLETE, completeHandler);
statusMsg="Uploading...";
}
catch (error:Error)
{
statusMsg = "Unable to upload file.";
}
}
private function selectHandler(event:Event):void {
statusMsg = "Selected " + fileRef.name;
btnUpload.enabled=true;
}
private function completeHandler(event:Event):void
{
statusMsg = "File Uploaded";
btnUpload.enabled=false;
}
]]>
</mx:Script>
<mx:VBox width="100%" height="100%">
<mx:Form>
<mx:FormHeading label="Uploading a file to a remote server" />
<mx:FormItem label="1.">
<mx:Button click="selectFile()" label="Select A file" horizontalCenter="0" id="btnSelect" />
</mx:FormItem>
<mx:FormItem label="2.">
<mx:Button click="uploadFile()" label="Upload" horizontalCenter="0" enabled="false" id="btnUpload" />
</mx:FormItem>
</mx:Form>
<mx:Label text="Status: {statusMsg}" />
</mx:VBox>
</mx:WindowedApplication>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" height="150" width="300">
<mx:Script>
<![CDATA[
import flash.net.FileReference;
import flash.events.*;
private var fileRef:FileReference = new FileReference();Çspan style='color: #808080'ÈÇemÈ //Used to hold the file we are uploading
Ç/emÈÇ/spanÈ private var uploadURL:String = "http://127.0.0.1/projects/air/AirFileUpload/bin/upload.cfm";Çspan style='color: #808080'ÈÇemÈ // URL for server side upload script
Ç/emÈÇ/spanÈ
[Bindable]
private var statusMsg:String = "No file Selected";Çspan style='color: #808080'ÈÇemÈ // Used in status label
Ç/emÈÇ/spanÈ
/* Activate the OS file selection menu */
private function selectFile():void {
try
{
var success:Boolean = fileRef.browse();
fileRef.addEventListener(Event.SELECT, selectHandler);
}
catch (error:Error)
{
trace("Unable to browse for files.");
}
}
/* Uploads the file to the server */
private function uploadFile():void {
var request:URLRequest = new URLRequest();
request.url = uploadURL;
request.method=URLRequestMethod.POST;
try
{
fileRef.upload(request,"Filedata");
fileRef.addEventListener(Event.COMPLETE, completeHandler);
statusMsg="Uploading...";
}
catch (error:Error)
{
statusMsg = "Unable to upload file.";
}
}
private function selectHandler(event:Event):void {
statusMsg = "Selected " + fileRef.name;
btnUpload.enabled=true;
}
private function completeHandler(event:Event):void
{
statusMsg = "File Uploaded";
btnUpload.enabled=false;
}
]]>
</mx:Script>
<mx:VBox width="100%" height="100%">
<mx:Form>
<mx:FormHeading label="Uploading a file to a remote server" />
<mx:FormItem label="1.">
<mx:Button click="selectFile()" label="Select A file" horizontalCenter="0" id="btnSelect" />
</mx:FormItem>
<mx:FormItem label="2.">
<mx:Button click="uploadFile()" label="Upload" horizontalCenter="0" enabled="false" id="btnUpload" />
</mx:FormItem>
</mx:Form>
<mx:Label text="Status: {statusMsg}" />
</mx:VBox>
</mx:WindowedApplication>
upload.cfm
<cffile action="upload" filefield="Filedata" destination="#ExpandPath('.')#" nameconflict="OVERWRITE" />



![Validate my RSS feed [Valid RSS]](http://www.shlomygantz.com/blog/valid-rss.png)
JD
" request.method = URLRequestMethod.POST; "
This is important.
Thank you !
http://www.dir-9.com
i run a filereference.upload test flex web-application which uploads a file through a coldfusion server.
i’m using flex 3 and coldfusion 9.
i’m also passing parameters in the URLRequest that points to the upload cfm file.
when i run my test using the built-in web server (Jrun) all seems to work just fine
when i run the same test on a coldfusion instance that runs on Apache web server, it throws a I/O Error…
i suspect i’m missing some kind of a configuration i need to perform when moving to a production setup rather then a development environment…
can anyone guide me through this?
does it have to do with security? on the client? on the server?
i’m still running on localhost…
the code is very similar to yours
thank you for your time
cheers
Yariv