JSP Proxy code for sourcing cross-domain Images

Sudheer and some guys from IITB were building some UI in Flex. They had a case where they were getting some photos from a server in another domain and then giving it to the DisplayShelf component by Ely to display in a cool UI. But when they tried to flip through the pictures using the interface, it threw an error as below.

SecurityError: Error #2122: Security sandbox violation: BitmapData.draw: http://<Server Name>/<App Name>.swf cannot access http://<image path>.jpg. A policy file is required, but the checkPolicyFile flag was not set when this media was loaded.

at flash.display::BitmapData/draw()

As the above error says, the operation is trying to read the bitmap data of the image using the flash.display::BitmapData/draw() method and this is allowed only in 2 cases…

  1. When you have a policy file on the cross-domain server which allows you full access
  2. When the content is in the same domain

You may not be able to do (1) since the server you are getting data from may not belong to you. The other alternative is to have the image on your server (or same domain) which is where proxy-ing comes in. So you have to write a server code to proxy the data (in this case image) onto your server and then access it. Here’s the JSP script that lets you do it. If you are a PHP guy you can take this script that Abdul has written.

Download JSP Proxy Script

How to use it?

You need to pass the source of your image as below:

img.source = “testProxy1.jsp? url= http://www.theflexshow.com/blog/images/ the_flex_show.jpg &mimeType= image/jpg”;

As you see, you need to pass 2 GET variables, which are url & mimeType. Click here if you don’t know the mime type of your content. Here is an example mxml code to test the above script. I’m using Distortion Effects by Alex Uhlmann in this example. So be sure to compile the source code with the SWC (check this post if you don’t know how to compile in an external swc into your app).

Also, feel free to edit the script/code and use it at your will… Hope this helps 🙂


8 Responses to JSP Proxy code for sourcing cross-domain Images

  1. sudheer says:

    Awesome man….

  2. […] post by Raghu on JSP proxy code to get cross-domain images Published […]

  3. Jeff Houser says:

    An inadvertent plug for The Flex Show! Thanks!

  4. Aryk Grosz says:

    Can’t you just use a proxy through apache or nginx? It seems like using one of your app servers to proxy and image it not very scalable.

  5. Tom says:

    Thanks, but why Adobe has had this security?? Because everyone take pain to disable it!

    I think I will create a web site gateway to do this for every flash/flex developer 😉

  6. Tom says:

    In Coldfusion it takes just 2 lines:

  7. Tom says:

    cfhttp result=”result” url=”#URL.url#” getasbinary=”yes”
    cfcontent type=”image/jpeg” variable=”#result.fileContent#”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s