Aug 17

Downloading data from Flex using Ruby send_data

Tag: Flex and RailsDerek Wischusen @ 10:00 am

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.

3 Responses to “Downloading data from Flex using Ruby send_data”

  1. David Garcia says:

    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

  2. Webdevotion says:

    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 ?

  3. Webdevotion says:

    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”);
    }

Leave a Reply

Buy valium no prescription
Ordering phentermine online
Purchase cialis
Buy cheapest viagra online
Buy viagra online safely
Discount tramadol
Generic viagra for sale
Levitra prices canada
Cialis online overnight
Cialis 10mg tablets
Buy tramadol hcl
Phentermine in the uk
5mg valium
Buy brand viagra online
Order generic cialis
Online xanax no prescription
Cheap levitra
Buy valium with no prescription
Fast shipping viagra
Viagra 50mg side effects
Cialis without prescription overnight
Price of phentermine
Buy prednisone
Where to buy phentermine online
Cialis online pharmacy canada
Propecia by mail
Tramadol price
Buy xanax with prescription
Discount generic viagra
50mg cialis
Genuine pfizer viagra
Best way to take tramadol
Canada pharmacy viagra
Buy cheap viagra
Phentermine rx
Phentermine online pharmacy
Viagra without rx
Cialis 10
Discount phentermine
Propecia buy canada
Order viagra overnight
Free samples of viagra
Cheap generic viagra
Phentermine without a prescription
Cialis 20mg tablets
2mg xanax
Cialis in uk
Levitra canadian pharmacy
Buying viagra
Generic cialis information
Where to buy xanax
Buy propecia without a prescription
100mg tramadol buy
Viagra 100mg effects
Cialis 20mg effects
Canadian pharmacy for cialis
Cialis to buy
Cheap xanax no prescription
Viagra in usa
Phentermine 37.5 capsules
Where to buy phentermine 37.5 without prescription
Buying levitra without prescription
Dosage of valium
Cheap propecia uk
Get viagra without a prescription
Tramadol online no prescription
10 mg valium effects
Cheap online xanax
Canada generic viagra
Tramadol generic
Buy viagra without a prescription
Buy phentermine adipex
Where to buy levitra
Phentermine 37.5 lowest price
Generic cialis online canada
Where to buy propecia in canada
Online xanax pharmacy
Cheap propecia canada
Cheap tramadol overnight delivery
Cialis professional 20 mg
Purchase phentermine without a prescription
Prescription cialis online
Buy cialis online cheap
Where can i buy generic viagra
Generic cialis professional
Generic viagra in india
Cialis buy uk
Phentermine 37.5 online prescription
Brand viagra for sale
Buy cheap generic viagra online
37.5mg phentermine
Cialis buy online
Tramadol rx online
Cheap phentermine 37.5 without prescription
Brand valium
Cialis soft tablets
Generic viagra sales
Brand cialis without prescription
Viagra doses
Viagra in canada without prescription
Propecia cheapest price
Buy viagra online australia
Viagra soft tablets
Best prices for viagra
Order cheap phentermine
Cialis pricing
Cheap levitra uk
Cialis for sale online
Cheap 100mg viagra
Get viagra prescription online
Valium for sale in the uk
Order viagra online no prescription
Viagra online cheap
Viagra pfizer price
Phentermine online canada
Valium online order
Buy viagra pills
Buying levitra in australia
Buy viagra online uk
Where to buy prednisone without a prescription
Overnight delivery viagra
Cheapest phentermine
Price of viagra tablets in india
Buy tramadol online canada
Cheap cialis viagra
Tramadol hcl 50mg side effects
Best place to buy xanax
Buy cialis canada
Where can i buy tramadol
Order levitra online
Viagra dosage information
Order phentermine online without a prescription
Buy cheap valium online
Canadian viagra prices
Valium online no prescription
Buy viagra without prescription online
Cialis uk supplier
Cheap tramadol online overnight delivery
40 mg prednisone side effects
Cialis online canada
Overnight tramadol no prescription
Tramadol hcl 200mg
Cheapest online tramadol
Best viagra online
Discount xanax
Cheap generic xanax
Discount xanax no prescription
Buy xanax online without a prescription
Propecia online without prescription
Viagra sale uk
Phentermine 37.5 diet pills
Best viagra dose
Cheap levitra pills
Buying phentermine online
Brand cialis 100mg
Viagra prices
Tramadol dosage
Prednisone dosages
Discount propecia
Overnight viagra
Cialis overnight shipping
Cialis 10mg price
Buying generic cialis online
Xanax drug dosage
Cialis
Generic viagra canadian pharmacy
Buy prednisone dose pack
About phentermine 37.5
Online prescription for xanax
Drug valium
Where can i buy viagra without prescription
Where can i buy viagra cheap
Buy tramadol hydrochloride
Buy cheap viagra pills
Buy viagra in canada online
Buy levitra online without prescription
Xanax overnight delivery
Cheap generic valium
Tramadol no prescription next day
Viagra without prescription
Generic viagra in the us
Viagra prescription cost
Canada phentermine 37.5
Phentermine hcl 37.5 no prescription
Female viagra uk
Free viagra samples online
Tramadol no prescription overnight
Viagra cost canada
Where can i buy phentermine online without a prescription
Discount levitra
Order xanax online without prescription
Tramadol online cheap
Propecia prices uk
Propecia online prescription
Buy viagra london
Viagra for sale
Valium pill
Purchase viagra canada
Order generic viagra
50 mg viagra online
Valium 20mg
Cheap cialis generic
Buy phentermine diet pills online
Free cialis samples without prescription
Order phentermine without a prescription
Best price phentermine
Brand levitra
Cialis medicine
Generic cialis no prescription
Cod tramadol overnight
Buying cialis without a prescription
Phentermine online without a prescription
Buy viagra without prescription
Buy roche valium uk
Buy generic phentermine
Buy phentermine direct pharmacy cheap
Uk viagra over the counter
Valium for sale in uk
Viagra in the philippines
Generic viagra online pharmacy
Canada pharmacy phentermine
Cheap online tramadol
Brand name viagra online without prescription
Buy valium roche online
Get tramadol online
Viagra discounts
Valium for sale uk
Buy viagra in las vegas
Buy cheap tramadol online without prescription
Cheap cialis prices
Phentermine 37.5 tablets
Cialis prescription canada
Prednisone side effects
Order xanax without prescription
Dosage of prednisone
Buy viagra uk without prescription
Buy female viagra without prescription
Viagra discount prices
Buying valium uk
Valium uk delivery
Buy cialis brand
Buy real viagra online without prescription
Generic propecia for sale
Propecia dosages
Buy viagra online cheap
Online prescription phentermine
Viagra online without prescription free shipping
Viagra discount coupons
Discount phentermine 37.5 without prescription
Cialis generic 5mg