views:

508

answers:

4

I have a situation where I'm loading some content using a URLLoader but the content could either be a video (flv or swf) an image(jpg or png or gif) or a swf. The trick is, I need to react in different ways based on the type of content that's loaded without necessarily knowing what type of content it is beforehand.

I currently have concocted an elaborate solution (and by elaborate I mean a confusing series of if statements and try...catch blocks) involving trying to either load it into a control and catching the errors (for example: I'll create a Image instance, add an event listener for the ioError event and it's recieved I assume it's not an image or a swf...) but it kinda seems like a hack.

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

+1  A: 

You should probably be looking at the headers to determine the file type.

e.g. all SWF files should start with either 0x46, 0x57, 0x53 (“FWS”), or 0x43, 0x57, 0x53 ("CWS") if they're compressed.

These would be bytes 0 to 2 in the URLLoader.data array.

inferis
A: 

Sadly it looks like if you're not using AIR, then you don't (at least today) get access to the HTTP response (i.e., URLLoader.httpResponseStatus) headers. So your "concocted" approach (which I've actually used myself!) is probably a suitable one for the time being, as is the previous poster's.

Looks like there's an open issue at Adobe for it, though:

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

...so at least there's that. :)

Christian Nunciato
+1  A: 

Take a look at: http://code.google.com/p/as3httpclient/wiki/Links to determine response headers. I used it for another purpose and it worked well.

Brandon
A: 

maybe something like

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