- FIDbg10.ocx crashed while loading file
原以為FileReference的物件可以像在使用save()函式時宣告在method裡,沒想到這樣子會造成FIDbg10.ocx掛掉然後連帶著瀏覽器也被關閉。下面是一個會當掉的寫法:
package{
import flash.display.Sprite;
import flash.net.FileReference;
import flash.events.Event;
import flash.events.MouseEvent;
import com.xinyu.button.TextButton;
public class LoadFileDemo extends Sprite{
private var loadBtn:TextButton;
public function LoadFileDemo(){
loadBtn = new TextButton(80,20,10,1,0xCCCCCC,"Load",0x000000);
addChild(loadBtn);
loadBtn.addEventListener(MouseEvent.CLICK, onClicked);
}
private function onClicked(event:MouseEvent):void{
//declare the FileReference object inside the method that would lead to browser crash
var file:FileReference = new FileReference();
file.addEventListener(Event.SELECT, onFileSelected);
file.addEventListener(Event.COMPLETE, onFileLoaded);
file.browse();
}
private function onFileSelected(event:Event):void{
event.target.load();
}
private function onFileLoaded(event:Event):void{
trace(event.target.size);
}
}
}
可以看到上面的範例程式碼裡,我將FileReference的物件宣告在onClicked()這個Event Handler裡,拿去編譯執行後程式就會當掉。為了要解決這個問題,寫法要改成這樣:
package{
import flash.display.Sprite;
import flash.net.FileReference;
import flash.events.Event;
import flash.events.MouseEvent;
import com.xinyu.button.TextButton;
public class LoadFileDemo extends Sprite{
private var loadBtn:TextButton;
private var file:FileReference;
public function LoadFileDemo(){
loadBtn = new TextButton(80,20,10,1,0xCCCCCC,"Load",0x000000);
addChild(loadBtn);
loadBtn.addEventListener(MouseEvent.CLICK, onClicked);
}
private function onClicked(event:MouseEvent):void{
file = new FileReference();
file.addEventListener(Event.SELECT, onFileSelected);
file.addEventListener(Event.COMPLETE, onFileLoaded);
file.browse();
}
private function onFileSelected(event:Event):void{
event.target.load();
}
private function onFileLoaded(event:Event):void{
trace(event.target.size);
}
}
}
No comments:
Post a Comment