Flash 10 Filereference新增了Save和Load,可讓使用者不必透過檔案上傳到伺服器回傳,直接就可存取檔案,使用起來方便許多,寫給簡單的範例給大家做參考。
值得要注意的是 "browse"只能透過使用者觸發,也就是不能直接呼叫,要為click之類的事件觸發,並在browse之後才做上傳的動作,這部分是為了保護非法的存取
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
applicationComplete="main();"
width="640" height="480">
<mx:Script>
<![CDATA[
import mx.graphics.codec.JPEGEncoder;
import mx.core.UIComponent;
import flash.net.FileReference;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.display.BitmapData;
import mx.controls.Alert;
private var fileRef:FileReference;
private var saveFileRef:FileReference;
private var filterArray:Array = [];
private var loader:Loader;
private function main():void
{
var container:UIComponent = new UIComponent();
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleteHandler);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loadErrorHandler);
fileRef = new FileReference();
filterArray.push(new FileFilter("JPEG", "*.jpg"));
filterArray.push(new FileFilter("GIF", "*.gif"));
filterArray.push(new FileFilter("PNG", "*.png"));
filterArray.push(new FileFilter("SWF", "*.swf"));
saveFileRef = new FileReference();
saveFileRef.addEventListener(Event.OPEN, fileOpenHandler);
saveFileRef.addEventListener(Event.CANCEL, cancelHandler);
saveFileRef.addEventListener(Event.COMPLETE, saveCompleteHandler);
fileRef.addEventListener(Event.COMPLETE, fileCompleteHandler);
fileRef.addEventListener(Event.OPEN, fileOpenHandler);
fileRef.addEventListener(Event.SELECT, fileSelectHandler);
fileRef.addEventListener(Event.CANCEL, cancelHandler);
container.addChild(loader);
this.addChildAt(container, 0);
}
private function loadCompleteHandler(event:Event):void
{
myMsg.text += "loader complete\n";
}
private function loadErrorHandler(event:IOErrorEvent):void
{
myMsg.text += "IOError\n";
}
private function fileCompleteHandler(event:Event):void
{
myMsg.text += "complete\n";
var tmpFileRef:FileReference = event.currentTarget as FileReference;
loader.unloadAndStop(true);
loader.loadBytes(tmpFileRef.data);
}
private function fileOpenHandler(event:Event):void
{
myMsg.text += "open\n";
}
private function fileSelectHandler(event:Event):void
{
myMsg.text += "select\n";
//選取後讀入
var tmpFileRef:FileReference = event.currentTarget as FileReference;
tmpFileRef.load();
}
private function uploadHandler(event:MouseEvent):void
{
//正常叫用
fileRef.browse(filterArray);
}
private function cancelHandler(event:Event):void
{
myMsg.text += "cancel\n";
}
private function saveCompleteHandler(event:Event):void
{
myMsg.text += "save complete\n";
}
private function saveHandler(event:MouseEvent):void
{
if (loader.content)
{
var bitmapData:BitmapData = new BitmapData(loader.width, loader.height);
var encoder:JPEGEncoder = new JPEGEncoder(90);
var byteArray:ByteArray;
bitmapData.draw(loader);
byteArray = encoder.encode(bitmapData);
saveFileRef.save(byteArray, "test.jpg");
}
else
{
Alert.show("請先Upload");
}
}
]]>
</mx:Script>
<mx:TextArea x="124.25" y="369.95" width="465.8333" height="100.98485" id="myMsg"/>
<mx:Button x="35.55" y="391.6" label="Upload" click="uploadHandler(event);"/>
<mx:Button x="35.55" y="421.2" label="Save" click="saveHandler(event);" width="65" height="21.969696"/>
</mx:Application>
在AIR的File類別也繼承自Filereference,所以File也能使用Load和Save,用法跟 AIR的FileStream不同,以後有機會再來介紹