I’m building a Flex application where users can enter a URL to an image on the Internet and then have flash load the image that the URL points to. This works fine for URLs with no special characters (by either setting the source property or calling load() on Image). However, when a URL is submitted that has special characters which have already been escaped, i.e. in most cases simply copied directly from the URL bar in a browser, URLRequest escapes the percent signs again with ‘%25’, which makes the URL invalid and the server returns an error. Pasting the same URL in a browser loads the image just fine though. I’ve tried decodeURI() on the URL, but that ignores a large set of characters, so I tried decodeURIComponent(), and that decodes everything correctly, but when URLRequest goes to re-encode it, it ignores the now-decoded special characters and doesn’t escape them, so in this case the URL is not valid, because it still has unsafe characters in it and fails. So, how can I send URLRequest something with special characters in it (encoded or unencoded) and have it actually work? Really, I wish it didn’t do anything with my URL and let me determine if it’s valid or not, but it’s embedded in the Flash Player globals swc and is therefore the closed-source piece of flash and I can’t see the source code. Any help would be greatly appreciated!
This is basically what I’m doing:
var image:Image = new Image();
image.source = textInput.text;
And the URL I’ve been testing with is from Getty images:
If you step through the code, in the loadContent() function in SWFLoader (which Image extends), after requestedURL is created from a new URLRequest, the url property on the URLRequest object has all percent signs escaped (again) with ‘%25’, producing the following URL (which will return a 500 error from Getty):
Update
- I forgot to mention that I’m running Flex SDK 4.1, and the
Imageismx.controls.Image - Based on wvxvw’s suggestion, I tried
unescape()with the same result as usingdecodeURIComponent() - I also tried calling
encodeURI()on the URL after decoding withdecodeURIComponent(), but of course it ignores many of the unsafe characters that are in the decoded URL and doesn’t re-encode them.URLRequestdoes the same, so the URL called in the end isn’t encoded and therefore fails. - Interestingly too, when I tried calling
encodeURIComponent()on the URL and re-encoding it before passing it in,URLRequestdid not re-encode the percents, presumably because they’re already ‘%25’? - In the end, it seems to only look for ‘%’ signs and simply replaces them with ‘%25′ (if they aren’t followed already by ’25’) and then ignores the other unsafe characters. Why would it do that? And why hasn’t anyone else run into this problem before?
For images loaded from URLs with potentially unsafe characters, you must use
LoaderorURLLoaderand load them manually, which avoidsSWFLoaderand theOSToPlayerURI()function.