Thursday, September 10, 2009

POST with navigateToUrl in Flex

Been struggling from the last 2 days to get a POST to work with navigateToUrl in Internet Explorer.

Sample Code:

var vars:URLVariables = new URLVariables();
vars.action = "some_action";
vars.ZZ_RESPONSE_CONTENT_TYPE = "application/x-download"
vars.ZZ_RESPONSE_HEADER = "Content-Disposition|attachment; filename=\"" +"abc.csv\"";
vars.arg1 = _arg1
vars.arg2 = _arg2

var request:URLRequest = new URLRequest();
request.data = vars;
request.method = URLRequestMethod.POST;
request.url = Server_URL

navigateToURL(request, "_blank");

What I am trying to do is POST some data to the server via a URLRequest and download a file based on the data. I use the navigateToUrl function and pass the request and ask it to open up the download in a new browser window. Everything works well in all browsers when my HTML page moves straight into the server url.
The problem arises when I use an IFrame in the HTML (to mask my server url). The code still works fine with other browsers. But Mr. IE has a problem. It does not get me my download file. On scraping through the logs I found that, it was not passing the parameters to the server and hence the action failed. (Confirmed this from a couple of blogs too).

WorkAround: Don't use IFrame (pretty simple if you don't mind the users looking at the URL changing in their address bar)

OR

use the "_top/_self/_parent" option with the navigate to URL

The latter, however, might again be a hindrance, if the Information Bar in the Internet Explorer is enabled as it tries to reload the page as soon as we try to download from the Information Bar.

I want to try and remove the Information Bar programatically, but have no clue about it till now. Will update as soon as I find something.

All help welcome.

2 comments :

Anonymous said...

And here is the solution...
You must first open a window for the content to go in. You by invoking javascript using the ExternalInterface class. The javascript function must be defined in your page. The following is javascript..

function openWindow(){
window.open("about:blank", "windowName")
}

After this window is open, you then make a call to navigateToUrl using the window you created as the second parameter.

Flex Code...
var urlRequest:UrlRequest = ...
ExternalInterface.call("openWindow");
navigateToUrl(urlRequest, "windowName")

Pramod Rao said...

Thanks dude. Actually wasn't looking to use JavaScript in the system. It's made only with Flex. With JSP definitely things become simpler. Thanks again.