Flex:FileReference 和 Image 未处理 IOErrorEvent
以下代码显示了一个按钮,允许您选择一个文件(应该是图像)并将其显示到图像组件中。当我选择无效图像(例如不支持的图像类型、Word 文档)时,出现以下错误:
“错误#2044:未处理的 IOErrorEvent:.text=错误#2124:加载的文件是未知类型。”< /strong>
我知道我可以将 FileFilter 传递给 FileReference:browse 调用,但这超出了重点。我的问题是...我想自己处理 IOErrorEvent,我缺少什么事件侦听器?
private var file:FileReference = new FileReference();
private function onBrowse():void {
file.browse(null);
file.addEventListener(Event.SELECT, handleFileSelect);
file.addEventListener(Event.COMPLETE, handleFileComplete);
file.addEventListener(IOErrorEvent.IO_ERROR, handleFileIoError);
}
private function handleFileSelect(event:Event):void {
file.load();
}
private function handleFileComplete(event:Event):void {
myImage.source = file.data;
}
private function handleFileIoError(event:Event):void {
Alert.show("handleFileIoError");
}
private function handleImageIoError(evt:IOErrorEvent):void {
Alert.show("handleImageIoError");
}
<mx:Button click="onBrowse()" label="Browse"/>
<mx:Image id="myImage" width="100" height="100" ioError="handleImageIoError(event)"/>
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. unsupported image type, 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);
file.addEventListener(IOErrorEvent.IO_ERROR, handleFileIoError);
}
private function handleFileSelect(event:Event):void {
file.load();
}
private function handleFileComplete(event:Event):void {
myImage.source = file.data;
}
private function handleFileIoError(event:Event):void {
Alert.show("handleFileIoError");
}
private function handleImageIoError(evt:IOErrorEvent):void {
Alert.show("handleImageIoError");
}
<mx:Button click="onBrowse()" label="Browse"/>
<mx:Image id="myImage" width="100" height="100" ioError="handleImageIoError(event)"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,这是 Flex SDK 的一个错误。看一下 SWFLoader 的 loadContent 方法。它没有针对多种情况(包括您的情况)的错误处理程序。该错误已在 SDK 3.4 中修复,因此进行更新可能是个好主意。
Unfortunately it is a bug from Flex SDK. Take a look on SWFLoader, method loadContent. It does not have a handler for errors for several cases (your included). The bug is fixed in SDK 3.4 , so it maybe a good idea to do an update.