Aug 17
Downloading data from Flex using Ruby send_data
I recently encountered a situation where I wanted to be able to download data from Flex directly to the user’s desktop. Fortunately, Ruby provides a very simple way to do this using the send_data method, which sends data to the browser for download. However, the way that you need to communicate with this method for it work properly is slightly different from how you would normally communicate between Flex and Rails.Â
Usually, when you want to call a Ruby method you would use an HTTPService, but when you are calling send_data this will not produce the desired result. This is because the data will be returned back to the HTTPService inside of Flex, which is pretty useless (you are basically passing the data back and forth). What you want is for the data to be returned to the browser so that the user can download it.
To solve this problem I used the Flex navigateToURL method along with a URLVariables object:
 private function saveToFile():void
  {
  var appUrl:URLRequest = new URLRequest();
  appUrl.url = "application/data_to_file";
  appUrl.method = URLRequestMethod.POST;
 Â
  var variables:URLVariables = new URLVariables();
  variables.content = "Downloaded from Flex!";
  variables.suggested_name = "download.txt";
          Â
  appUrl.data = variables; Â
 navigateToURL(appUrl, "_top");
}
The URLRequest object specifies the url of the method to call (the data_to_file method inside the application controller), the URLVariables object specifies the data to pass, and the navigateToURL method sends HTTP request through the browswer to data_to_file, which sends the data back to your browswer, which prompts you to download the file.
Here is the Ruby method:
    def data_to_file
     file_data = params[:content]
     suggestion = params[:suggested_name]
     send_data(file_data, :filename => suggestion)
   end
To see a simple example of this, check out the following post.

October 11th, 2006 at 4:52 pm
Dear Derek,
I am working on a photo capture webpart and I´m trying to create an image but haven´t been able to succeed. I have been researching to see how to create the image and that´s how I found your work. I was wondering if you could help me out with information on creating images on Flex.
Your help will be very much apretiated.
Best Regards,
David
February 21st, 2007 at 3:42 pm
Thanks for the nice example.
Is it possible to use it in conjunction with a FileReference so you can handle the download in the Flash player ?
February 21st, 2007 at 3:54 pm
nm,
I used this ruby code:
======================
class DownloadController ‘audio/mpeg3′
end
end
and this actionscript:
======================
var appUrl:URLRequest = new URLRequest();
appUrl.url = “http://www.domain.com/download”;
appUrl.method = URLRequestMethod.POST;
var variables:URLVariables = new URLVariables();
variables.file = tracks[selectedIndex].url;
appUrl.data = variables;
fileReference = new FileReference( );
try{
fileReference.download( appUrl );
fileReference.addEventListener(ProgressEvent.PROGRESS,utils.setProgress,false,0,true);
}
catch( illegal:IllegalOperationError )
{
trace(”illegal”);
}
catch( security:SecurityError )
{
trace(”security”);
}