AIR を使用しなくても、Flex または純粋な ActionScript プロジェクトでFileReferenceクラスを使用して、ローカル ファイルをアップロードするためのファイル参照ダイアログを表示できます。
これは、サービス クラスに抽象化できます。
応用
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script>
<![CDATA[
protected function buttonClickHandler(event:MouseEvent):void
{
var uploadService:UploadService = new UploadService("http://destination.com");
uploadService.browse();
}
]]>
</fx:Script>
<s:Button label="Upload..."
click="buttonClickHandler(event)" />
</s:Application>
アップロード サービス
package
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.URLRequest;
public class UploadService extends EventDispatcher
{
public var fileReference:FileReference;
public var fileTypes:FileFilter;
public var uri:String
public function UploadService(uri:String)
{
this.uri = uri;
}
public function browse():void
{
fileTypes = new FileFilter("* (*.*)", "*.*;");
var allTypes:Array = new Array(fileTypes);
fileReference = new FileReference();
fileReference.addEventListener(Event.SELECT, fileSelectHandler);
fileReference.addEventListener(Event.OPEN, fileOpenHandler);
fileReference.addEventListener(Event.COMPLETE, fileCompleteHandler);
fileReference.addEventListener(SecurityErrorEvent.SECURITY_ERROR, fileSecurityErrorHandler);
fileReference.addEventListener(IOErrorEvent.IO_ERROR, fileIoErrorHandler);
fileReference.browse(allTypes);
}
protected function fileSelectHandler(event:Event):void
{
fileReference.upload(new URLRequest(uri));
}
protected function fileOpenHandler(event:Event):void
{
dispatchEvent(new Event(Event.OPEN));
}
protected function fileCompleteHandler(event:Event):void
{
fileReference.removeEventListener(Event.SELECT, fileSelectHandler);
fileReference.removeEventListener(Event.OPEN, fileOpenHandler);
fileReference.removeEventListener(Event.COMPLETE, fileCompleteHandler);
fileReference.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, fileSecurityErrorHandler);
fileReference.removeEventListener(IOErrorEvent.IO_ERROR, fileIoErrorHandler);
dispatchEvent(new Event(Event.COMPLETE));
}
protected function fileSecurityErrorHandler(event:SecurityErrorEvent):void
{
dispatchEvent(new SecurityErrorEvent(SecurityErrorEvent.SECURITY_ERROR));
}
protected function fileIoErrorHandler(event:IOErrorEvent):void
{
dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR));
}
}
}