Flex: FileReference and Image unhandled IOErrorEvent
- by deux11
The following code shows a button that allows you to select a file (should be an image) and display it into an image component. When I select an invalid image (e.g. a word document), I get the following error:
"Error #2044: Unhandled IOErrorEvent:. text=Error #2124: Loaded file is an unknown type."
I know I can pass a FileFilter to the FileReference:browse call, but that's beyond the point. My question is... I want to handle the IOErrorEvent myself, what event listener am I missing?
private var file:FileReference = new FileReference();
private function onBrowse():void {
file.browse(null);
file.addEventListener(Event.SELECT, handleFileSelect);
file.addEventListener(Event.COMPLETE, handleFileComplete);
}
private function handleFileSelect(event:Event):void {
file.load();
}
private function handleFileComplete(event:Event):void {
myImage.source = file.data;
}
private function handleImageIoError(evt:IOErrorEvent):void {
Alert.show("IOErrorEvent");
}
<mx:Button click="onBrowse()" label="Browse"/>
<mx:Image id="myImage" width="100" height="100" ioError="handleImageIoError(event)"/>