Flex 3 Close UrlLoader throws exception
Posted
by gmoniey
on Stack Overflow
See other posts from Stack Overflow
or by gmoniey
Published on 2010-05-28T18:41:39Z
Indexed on
2010/05/30
2:02 UTC
Read the original article
Hit count: 343
I am trying to simulate a 'HEAD' method using UrlLoader; essentially, I just want to check for the presence of a file without downloading the entire thing. I figured I would just use HttpStatusEvent, but the following code throws an exception (one that I can't wrap in a try/catch block) when you run in debug mode.
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()">
<mx:Script>
<![CDATA[
private static const BIG_FILE:String = "http://www.archive.org/download/gspmovvideotestIMG0021mov/IMG_0021.mov";
private var _loader:URLLoader;
private function init():void {
_loader = new URLLoader();
_loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, statusHandler);
_loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
_loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
_loader.load(new URLRequest(BIG_FILE));
}
public function unload():void {
try {
_loader.close();
_loader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, statusHandler);
_loader.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
_loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
}
catch(error:Error) {
status.text = error.message;
}
}
private function errorHandler(event:Event):void {
status.text = "error";
unload();
}
private function statusHandler(event:HTTPStatusEvent):void {
if(event.status.toString().match(/^2/)) {
status.text = "success";
unload();
}
else {
errorHandler(event);
}
}
]]>
</mx:Script>
<mx:Label id="status" />
I tried using ProgressEvents instead, but it seems that some 404 pages return content, so the status event will correctly identify if the page exists.
Anyone have any ideas?
© Stack Overflow or respective owner