In Flash Action script 3, when you need to load text, you use a class called URLLoader
, and when you need to load an image (or .swf) you use a class called 'Loader.' As far as I know, loading a .bmp with URLLoader
is as useless as loading an .xml into a Loader - it doesn't compute.
I'm making a class that handles a queue of external assets to be loaded - but aside from splitting the target URL to check out the file extension, I can't figure out a good way to tell if each URL requires a URLLoader
or a Loader. At any rate, it's imaginable that a .php URL might return either an image or a document - so there's no way to count on the file names to dictate the right type of loader class to use.
Any ideas on how to reliably detect the right class for the job on a URL-by-URL basis?
well, the most tricky question, is determining the type of the target ...
89 50 4E 47 0D 0A 1A 0A
)? GIF (47 49 46 38 39 61
)? JPEG (FF E0
)? SWF ("FWS"
(funny, isn't it?))? anything else should be text or text based ... in the case of an image, load it into a Loader
with Loader::loadBytes
... be careful with SWF though ... you should only load graphicals SWFs like that ... in any other case, convert it to a String using the right encoding (ideally text data is served in utf8) ... then maybe you can already guess, whether it can be XML, JSON or URL-encoded variables ... try parsing (using classes XML
, com.adobe.serialization.JSON
, flash.net.URLVariables
) ... if everything fails, it's probably just text (you can try to verify that superficially ... if you want some input on that, leave me a comment) ...HTTP
yourself ... open a socket and load the source ... you will get mime-types in addition ... nothing you can rely on, but it helps ... there is an HTTP
implementation in AS3
... once you have the datathere is also an important difference between Loader and URLLoader ... Loader can load data accross domains, simply sandboxing its content so you cannot look into it ... URLLoader can only load from your domain, and domains that explicitely allow this using cross domain policy files ...
also, loading SWFs, that are not just external graphical assets, but you really want to interface with, you should not use this, since you need control over the LoaderContext etc. ...
so, yeah ... whatever way you choose, good luck ... ;)
greetz
back2dos