ActionScript: how to determine the type of loaded content?

I have a situation where I load some content using a URLLoader, but the content can be either video (flv or swf), or image (jpg or png or gif), or swf. The trick is that I need to react differently depending on the type of content being loaded, without knowing what type of content it is in advance.

Currently I have come up with a tricky solution (and by design I mean a convoluted sequence of if and try ... catch blocks) involving either trying to load it into the control and catch errors (ex: I will create an Image instance, add a listener events for the ioError event and get it, I'm guessing it's not an image or swf ...) but it looks like a hack.

Is there any clean way to determine the type of uploaded content from the URLLoader?

0


source to share


4 answers


Take a look at: http://code.google.com/p/as3httpclient/wiki/Links to determine the response headers. I've used it for other purposes and it worked well.



+1


source


You should probably look at the headers to determine the file type.

eg. all SWF files must start with 0x46, 0x57, 0x53 ("FWS") or 0x43, 0x57, 0x53 ("CWS") if compressed.



These will be bytes 0 through 2 in the URLLoader.data array.

+1


source


Unfortunately, it looks like if you're not using AIR, then you don't have (at least today) access to the HTTP response headers (i.e. URLLoader.httpResponseStatus). So your "contrived" approach (which I actually used myself!) Is probably as appropriate for the time being as the previous poster is.

It looks like there is an open issue for Adobe:

http://bugs.adobe.com/jira/browse/FP-251

... so at least there is that. :)

0


source


maybe something like

switch(your_request.url.split('.')[1]){
  case 'jpg':
  case 'swf':
}

      

0


source







All Articles