0

I want a user to press an excel button and get prompted to download an excel file. I normally do it like this:

var dest:String = excelEndpoint;

        var request:URLRequest = new URLRequest();
        request.url = dest;
        fr.download( request,'Locates.xls' );
        fr.addEventListener(HTTPStatusEvent.HTTP_STATUS, handleStatus);
        fr.addEventListener(IOErrorEvent.IO_ERROR, handleErr);

However, now I need to pass an object to the servlet. Seeing that you can't do that with URLRequest I tried using HTTPService:

var service:HTTPService = new HTTPService();
        service.url = excelEndpoint;
        service.method = "POST";
        service.showBusyCursor = true;
        service.addEventListener("result", httpResult);
        service.addEventListener("fault", httpFault);
        service.send( myObject);

Now I can get my data (myObject) to the servlet successfully, but I don't get prompted for a download.

How can I do that? Is it even possible with HTTPService?

Thanks for any helpful tips.

fumeng
  • 1,771
  • 5
  • 22
  • 61

2 Answers2

0

I don't think there is anything you can do w/ a remote request from the Flash Player to prompt the user to download something. The way I have done this in the past is two fold:

  1. Remote request pings server that generates the excel file and saves it to a temporary directory. The request returns a URL to the Flex app as the result of the call.

  2. When Flex app gets the result--a URL--it creates a URL request and pops it open in a new window, prompting the user to download the generated excel sheet.

    I don't see a way to do this in one shot; because the return value from the Flex HTTPService call goes to the Flash Player, not the browser.

Community
  • 1
  • 1
JeffryHouser
  • 39,401
  • 4
  • 38
  • 59
  • You can do it when you don't have to pass the Java servlet anything more complex than a bunch of key/value pairs. However, in my case, I need to pass a complex object; that's the rub. – fumeng Dec 13 '11 at 21:18
  • In your question you said you were already able to pass the complex object. I quote "Now I can get my data (myObject) to the servlet successfully". The question I read had nothing to do with properly passing data to the server. – JeffryHouser Dec 13 '11 at 21:23
  • Right: I can either prompt a download without passing complex data or pass complex data but not prompt a download....but not both. – fumeng Dec 13 '11 at 21:25
  • I'm not sure where the issue is. Do you have a problem w/ my proposed solution? Do you understand the difference between an HTTPService call and a URLRequest? URLRequest creates a new URL Request. HTTPService calls the remote server and gets a result. – JeffryHouser Dec 13 '11 at 22:02
  • Yes, I understand the difference. No, I wasn't keen on your approach even though it is perfectly valid; I wanted a way to do it in one shot. I've found a solution I'll post above. – fumeng Dec 14 '11 at 17:12
0

SOLVED:

I wanted to accomplish this with 1 remote call and the way I found to do this while passing an object that contained collections is this:

var uv:URLVariables = new URLVariables();
uv.typesColl = myObj.types.toString();
uv.partiesColl = myObj.parties.toString();
uv.statuses = myObj.statuses.toString();

Basically, create a property on the URLVariables object for each collection and then set all collections toString();

Hope that helps someone.

fumeng
  • 1,771
  • 5
  • 22
  • 61
  • Keep in mind the character limit of a URL String: http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url . As long as your object's string representation do not make the URL too long; you should be fine. – JeffryHouser Dec 14 '11 at 17:24