2

I'm trying to save an image from a Flash AS3 to my server as a jpg file. I found some PHP code to do this, but I want to do this in Ruby. I'm not quite sure how to convert the following code to Ruby. It's mainly the $GLOBALS["HTTP_RAW_POST_DATA"] part I don't know how to convert.

<?php

   if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) {

      // get bytearray $im = $GLOBALS["HTTP_RAW_POST_DATA"];

      // save image $f = fopen($_GET['name'], 'wb'); fwrite($f, $jpg); fclose($f);

   } else echo 'An error occured.';

?>

source:

http://designreviver.com/tutorials/actionscript-3-jpeg-encoder-revealed-saving-images-from-flash/

I tried to do the following, but the resulted image file dose not open

temp_file = Tempfile.new(['temp', '.jpg'], :encoding => 'ascii-8bit')
temp_file.write(request.raw_post)
temp_file.close

EDIT

The image data is in request.raw_post

EDIT2

As for the image being sent, here is how the header is created in AS3

var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
var saveJPG:URLRequest = new URLRequest("http://127.0.0.1:3000/save_image.xml");
saveJPG.requestHeaders.push(header);

saveJPG.method = URLRequestMethod.POST;

simo
  • 23,342
  • 38
  • 121
  • 218

1 Answers1

1
$GLOBALS["HTTP_RAW_POST_DATA"] 

can be grabbed from

request.env["HTTP_RAW_POST_DATA"]

and save it like https://stackoverflow.com/a/2571575/643500

Edit:

Isn't the image data in request.body ?

I need to see how the image is being sent. Do you have a header with the Content-Type?

Community
  • 1
  • 1
Sully
  • 14,672
  • 5
  • 54
  • 79
  • so, it shall work like this ? File.open(tempfile.path,'w') do |f| f.write request.env["HTTP_RAW_POST_DATA"] end – simo Oct 29 '12 at 16:43
  • You can test it and it will answer the question. Just debug what you have available. – Sully Oct 29 '12 at 16:52
  • The example in the link grabs the image data from the request. – Sully Oct 29 '12 at 17:00
  • I need to see how the image is being sent. Do you have a header with the `Content-Type`? – Sully Oct 30 '12 at 15:24
  • try Base64. If that does not help, then take a look at this example http://stackoverflow.com/a/6084208/643500 – Sully Oct 31 '12 at 06:54
  • Thanks, I've changed the way I post the image, its in multi-part post format now, which is better for paperclip – simo Oct 31 '12 at 07:40