1

I've got some problem with EventListener for loader:URLLoader. How can I determine if a file is already uploaded to the server or not?

var myRequest:URLRequest = new URLRequest("script.php");
loader.load(myRequest);
loader.addEventListener(Event.COMPLETE, redirect);

private function redirect(event:Event):void
{
navigateToURL(new URLRequest("http://example.com/"), "_self");
}
Kzw
  • 23
  • 1
  • 8

1 Answers1

3

How can I determine if a file is already uploaded to the server or not?

If you want to see if a file exists, then you can add a listener for an IOErrorEvent.IO_ERROR to the URLLoader alongside your listener for Event.COMPLETE.

var urlRequest:URLRequest = new URLRequest("http://bleh.com/file.php");
var urlLoader:URLLoader = new URLLoader(urlRequest);


urlLoader.addEventListener(Event.COMPLETE, complete);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, failure);


function complete(e:Event):void
{
    initialize(true);
}


function failure(e:IOErrorEvent):void
{
    initialize(false);
}


function initialize(fileExists:Boolean):void
{
    urlLoader.removeEventListener(Event.COMPLETE, complete);
    urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, failure);

    trace(fileExists);
}
Marty
  • 39,033
  • 19
  • 93
  • 162
  • This assumes the server checks for if the file exists and throws an error correct? The regular behavior if a server has write access will just be to overwrite the file otherwise (at least in the cases I can think of, Java or PHP). Believe it just requires that the header for http status be set to something other than 200 or 350, one of the 400s/500s http://stackoverflow.com/questions/3825990/http-response-code-for-post-when-resource-already-exists – shaunhusain Jun 18 '13 at 00:40
  • @shaunhusain Not sure to be honest, I just ran this and gave it a go with a couple files that I knew existed vs ones that didn't and it seemed to behave correctly. I'll look into it a little more. – Marty Jun 18 '13 at 00:43