Question

In Flex 3/AS 3, what would be a good way to check if a remote file exists? I'm thinking in PHP ways where you'd try to "fopen" a remote path (like "http://example.com/somefile.exe"), and see if it works or not. I'm not asking to just download all of the file, I just want to know if the file is there (and accessible).

Was it helpful?

Solution

You would probably need to attempt to load the file. If you get an IOError, the file doesn't exist (or your path is wrong). If it starts loading, by triggering a progress event then it exists. You can then cancel the remainder of the loading.

Alternatively you could try calling a PHP script from Flash which does what you have described, this could return a simple true/false.

OTHER TIPS

This is the best code I found for the job

var urlStream:URLStream = new URLStream();
urlStream.addEventListener(Event.OPEN, streamHandler);
urlStream.addEventListener(IOErrorEvent.IO_ERROR, streamHandler);
urlStream.load(new URLRequest("SOME_FILE"));

function streamHandler(e:Event):void {
    urlStream.close();
    if(e.type == Event.OPEN){
        trace("FILE EXISTS");
    } else if(e.type == IOErrorEvent.IO_ERROR){
        trace("FILE DOES NOT EXIST");
    }
}

There is a utility class I developed that handle a check weather a file exists or not. Here's the code: https://github.com/eladelrom/eladlib/blob/master/EladLibFlex/src/com/elad/framework/utils/FileExistsUtil.as

And implementation looks like this:

var fileExists:FileExistsUtil = new FileExistsUtil();
fileExists.checkFile("file.jpg", 
function(eventType:String):void
{
 trace(eventType);
}, 
function(errorType:String, text:String):void
{
 trace(errorType+": "+text);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top