在不使用 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));
}
}
}