# Application/octet-stream from flash: binary data, not swfupload

**URL:** <https://www.ruby-forum.com/t/application-octet-stream-from-flash-binary-data-not-swfupload/157766>\
**Category:** Rails\
**Created:** [January 15, 2009, 3:48am UTC](https://www.ruby-forum.com/t/application-octet-stream-from-flash-binary-data-not-swfupload/157766 "2009-01-15T03:48:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sarahg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a6a055/32.png) [@sarahg](https://www.ruby-forum.com/u/sarahg)\
**Post date:** [January 15, 2009, 3:48am UTC](https://www.ruby-forum.com/t/application-octet-stream-from-flash-binary-data-not-swfupload/157766/1 "2009-01-15T03:48:39Z")

</div>

Hello,

I’m working on a rails app with a flash application in it, and the  
flash application posts binary data to the rails app, to create and  
upload an image. The image arrives from Flash with a Content-Type of  
“application/octet-stream”, but by the time the image data hits the  
controller action, it is a string.

I have googled and found numerous solutions for similar cases: getting  
the content\_type of data uploaded from flash (generally via swfobject)  
and adjusting for when the content-type is application/octet-stream.  
But, these don’t work in my case – I’m guessing that all those cases  
are for actually uploading files rather than transporting binary  
data.

I do have mimetype\_fu installed and attachment\_fu hacks and the  
mime\_type gem but none of those solutions work because the data it’s  
working on in the controller is a string, and doesn’t have a  
content\_type method. All of these workarounds involve querying the  
data for its content\_type and adjusting from there.

Initially we thought “oh the form doesn’t have multipart set”, but  
when I track the data in request.rb (the  
parse\_multipart\_form\_parameters method), the data seems to be properly  
encoded with the correct form type:

The flash developer and I have done a lot of searching and it may be  
that the data needs to be sent differently from Flash (encoded in a  
different format perhaps, as a few blog posts suggest). But on the  
Rails end, does anyone have any insight on why the data is being  
thrown out by the time it gets to the controller? And, more  
importantly, where I might intercept it so by the time it hits the  
controller it is still application/octet-stream?

Below are some debug outputs if you want to see more details:

1. LOGGING from parse\_multipart\_form\_parameters in request.rb. This is  
the incoming request from Flash.

– Wed Jan 14 19:22:03 -0600 2009: here in  
parse\_multipart\_form\_parameters: body: #StringIO:0x3f03ab8 ;  
boundary: iuxcwgdgqyluhwtavtkiwakxwlyxfkxs ; body\_size: 65822 ; env:  
{“SERVER\_NAME”=\>“localhost”, “HTTP\_ENCTYPE”=\>“multipart/form-data”,  
“PATH\_INFO”=\>"/projects/13/elements/21/assets/130/visual\_notes",  
“CONTENT\_LENGTH”=\>“65822”, “HTTP\_CONTENT\_TYPE”=\>“multipart/form-data;  
boundary=iuxcwgdgqyluhwtavtkiwakxwlyxfkxs”,  
“HTTP\_ACCEPT\_ENCODING”=\>“gzip,deflate”, “HTTP\_USER\_AGENT”=\>“Mozilla/  
5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.5) Gecko/  
2008120121 Firefox/3.0.5”, “SCRIPT\_NAME”=\>"/",  
“SERVER\_PROTOCOL”=\>“HTTP/1.1”, “HTTP\_CACHE\_CONTROL”=\>“no-cache”,  
“HTTP\_ACCEPT\_LANGUAGE”=\>“en-us,en;q=0.5”, “HTTP\_HOST”=\>“localhost:  
3000”, “REMOTE\_ADDR”=\>“127.0.0.1”, “SERVER\_SOFTWARE”=\>“Mongrel 1.1.5”,  
“HTTP\_CONTENT\_LENGTH”=\>“65822”, “HTTP\_KEEP\_ALIVE”=\>“300”,  
“REQUEST\_PATH”=\>"/projects/13/elements/21/assets/130/visual\_notes",  
“CONTENT\_TYPE”=\>“multipart/form-data;  
boundary=iuxcwgdgqyluhwtavtkiwakxwlyxfkxs”, “HTTP\_REFERER”=\>“http://  
localhost:3000/movies/image\_creator.swf”, “HTTP\_COOKIE”=\>“last-visited-  
project=canvasband;  
\_canvasband\_session=BAh7CToMdXNlcl9pZGkMOgxjc3JmX2lkIiUyOTI3ZjJiMGJlMDhmNzMxMDZh  
%0AY2M4NjUyZGZmYWI5ZDoOcmV0dXJuX3RvMCIKZmxhc2hJQzonQWN0aW9uQ29u  
%0AdHJvbGxlcjo6Rmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA%3D%3D–  
dcc1e413220b4e43db3ab08a37cf67ff73f8ec6e; auth\_token=;  
mingle\_2\_1\_session\_id=4292f420b98099b491c469a9bffab2ac”,  
“HTTP\_ACCEPT\_CHARSET”=\>“ISO-8859-1,utf-8;q=0.7,_;q=0.7",  
“HTTP\_VERSION”=\>“HTTP/1.1”, “REQUEST\_URI”=\>"/projects/13/elements/21/  
assets/130/visual\_notes", “SERVER\_PORT”=\>“3000”,  
“GATEWAY\_INTERFACE”=\>“CGI/1.2”, “HTTP\_ACCEPT”=\>"text/html,application/  
xhtml+xml,application/xml;q=0.9,_/\*;q=0.8”, “HTTP\_CONNECTION”=\>“keep-  
alive”, “REQUEST\_METHOD”=\>“POST”}

1. A little later in read\_multipart, I can see that the data has  
arrived in the application/octet-stream format:

Content-Disposition: form-data; name=“image\_uploaded\_data”;  
filename=“1141922993.jpg”  
Content-Type: application/octet-stream

1. My controller action params

Parameters: `“element\_id”=\>“21”, “project\_id”=\>“13”,  
“action”=\>“create”,  
“authenticity\_token”=\>“d72daa6709e28c9ed1b55566419e1e8bd714da11”,  
“image\_uploaded\_data”=\>“1141951886.jpg”, “Upload”=\>“Submit Query”,  
“controller”=\>“projects/elements/assets/visual\_notes”,  
“visual\_note\_created\_by\_id”=\>“1”, “asset\_id”=\>"130

1. Output when running a before\_filter to  
before\_filter do |controller|  
logger.info(“CLASS: #{controller.params  
[:image\_uploaded\_data].class}”)  
end  
CLASS: String

Thanks,

Sarah

---

<div class="post-metadata">

**Author:** ![sarahg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a6a055/32.png) [@sarahg](https://www.ruby-forum.com/u/sarahg)\
**Post date:** [January 15, 2009, 10:11am UTC](https://www.ruby-forum.com/t/application-octet-stream-from-flash-binary-data-not-swfupload/157766/2 "2009-01-15T10:11:38Z")

</div>

On 15 Jan 2009, at 03:48, sarah wrote:

> I’m working on a rails app with a flash application in it, and the  
> flash application posts binary data to the rails app, to create and  
> upload an image. The image arrives from Flash with a Content-Type of  
> “application/octet-stream”, but by the time the image data hits the  
> controller action, it is a string.

That’s merely because of the filesize of the data you are sending.  
Everything below 16KB will be seen as StringIO, above it will be a  
TempFile. Attachment\_fu has everything in place to handle it.

Look at line 303 at

> <https://github.com/technoweenie/attachment_fu/blob/743a95be0f01696530f558e7f4934cc7e57d3ab8/lib/technoweenie/attachment_fu.rb>

> I have googled and found numerous solutions for similar cases: getting  
> the content\_type of data uploaded from flash (generally via swfobject)  
> and adjusting for when the content-type is application/octet-stream.  
> But, these don’t work in my case – I’m guessing that all those cases  
> are for actually uploading files rather than transporting binary  
> data.

Attachment\_fu combined with mimetype\_fu work perfectly for me with any  
data sent from Flash. Flash has no way to pass in the appropriate  
content type (simply said: it’s always application/octet-stream), but  
I can see in your controller that you have a filename available  
(xxxx.jpg). This should be enough for mimetype\_fu to determine the  
content-type as “image/jpeg”.

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![sarahg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a6a055/32.png) [@sarahg](https://www.ruby-forum.com/u/sarahg)\
**Post date:** [January 15, 2009, 7:56pm UTC](https://www.ruby-forum.com/t/application-octet-stream-from-flash-binary-data-not-swfupload/157766/3 "2009-01-15T19:56:08Z")

</div>

Thanks for the reply, Peter. Unfortunately, this is not what is  
happening in my application. Are you uploading images or passing  
binary data created in Flash to Rails?

What is happening for us is that I am able to get the content-type as  
“image/jpeg” using mimetype\_fu, but that is as far as things go. If I  
then try to save that data in the controller (using attachment\_fu),  
like so:

image\_params = {:uploaded\_data =\> params[‘image\_uploaded\_data’]}  
image = @visual\_note.build\_image(image\_params.merge(:created\_by =\>  
current\_user))  
if image.valid?  
#do stuff  
else  
#errors  
end

I get validates\_as\_attachment errors:  
Content type can’t be blank; Size can’t be blank; Size is not included  
in the list; Filename can’t be blank

* * *

When I use some attachment\_fu\_hacks that I found here:  
[http://seventytwo.co.uk/posts/making-swfupload-and-rails-work-together:](http://seventytwo.co.uk/posts/making-swfupload-and-rails-work-together:)

def uploaded\_data=(file\_data)  
return nil if file\_data.nil? || file\_data.size == 0  
self.content\_type = detect\_mimetype(file\_data)  
self.filename = file\_data.original\_filename if respond\_to?  
(:filename)  
if file\_data.is\_a?(StringIO)  
file\_data.rewind  
self.temp\_data = file\_data.read  
else  
self.temp\_path = file\_data.path  
end  
end

def detect\_mimetype(file\_data)  
if file\_data.content\_type.strip == “application/octet-stream”  
return File.mime\_type?(file\_data.original\_filename)  
else  
return file\_data.content\_type  
end  
end  
end

Errors are thrown because file\_data – the string passed in through my  
params[:image\_uploaded\_data] from Flash does not respond to  
content\_type or original\_filename

* * *

Am I missing something from the point where the I am receiving the  
data in the controller and getting it to attachment\_fu? It doesn’t  
know it is application/octet-stream because it doesn’t respond to  
content-type at the controller level.

Thanks,  
Sarah

---

<div class="post-metadata">

**Author:** ![sarahg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a6a055/32.png) [@sarahg](https://www.ruby-forum.com/u/sarahg)\
**Post date:** [January 16, 2009, 5:48am UTC](https://www.ruby-forum.com/t/application-octet-stream-from-flash-binary-data-not-swfupload/157766/4 "2009-01-16T05:48:16Z")

</div>

Interesting. This is definitely a step in a productive direction and I  
am very thankful for the suggestions that you have given. It’s not  
_quite_ doing the trick, but it’s the closest I’ve been so far.  
Here’s the scoop:

First, to do this, I had to make :content\_type  
and :filename :attr\_accessible and make a setter for temp\_data in my  
asset class, since it’s not a setter attribute. This method calls  
set\_temp\_data in attachment\_fu, like so:

def temp\_data=  
set\_temp\_data(data)  
end

I then followed your suggestion in the controller, like so, but  
getting the mime\_type from mimetype\_fu, in case it’s not always “image/  
jpeg”

image\_params = {:temp\_data =\> params  
[‘image\_uploaded\_data’], :filename=\> params  
[‘image\_uploaded\_data’], :content\_type =\> File.mime\_type?(params  
[‘image\_uploaded\_data’])}

Note that each of values is the same field: params  
[‘image\_uploaded\_data’] is the string of the filename that comes in.

At this point, I ran the code and for the first time the process  
worked: the image validated, and it was stored on s3. However, the  
image itself was not the image exported from Flash: it was a jpeg _of_  
the string of filename. You can see it here:  
[http://s3.amazonaws.com/canvasband\_development/assets/255/v1/1152215550.jpg](http://s3.amazonaws.com/canvasband_development/assets/255/v1/1152215550.jpg)

Weird. So I looked more closely at attachment\_fu, and saw that in the  
uploaded\_data= method, the seemingly relevant lines here pass the data  
from the file to set\_temp\_data, like so:

(line 334)  
if file\_data.is\_a?(StringIO)  
file\_data.rewind  
set\_temp\_data file\_data.read  
else  
self.temp\_paths.unshift file\_data  
end

So I switched my temp data setter to mimic that:  
def temp\_data=(data)  
data.rewind  
set\_temp\_data(data.read)  
end

and tried again, at which point I received the error:  
NoMethodError (undefined method `rewind’ for “1152233660.jpg”:String)

So I then tried to convert the string to a stringIO object,

def temp\_data=(data)  
datastr = StringIO.new(data)  
datastr.rewind  
set\_temp\_data(datastr.read)  
end

Which then worked again – ie, saved and uploaded the image – except  
the “image” it saved was still a jpeg of the filename, again visible  
on s3  
[http://s3.amazonaws.com/canvasband\_development/assets/263/v1/1152241736\_thumb.jpg](http://s3.amazonaws.com/canvasband_development/assets/263/v1/1152241736_thumb.jpg),  
but not what Flash sent in.

Which seems to bring me back to the original problem, namely: despite  
the fact that the binary data is coming into the application, by the  
time I’m in the controller I can’t figure out how to access that data,  
and am only managing to get this pseudo-image.

Is there anything else you can think of here?

Thanks again for your input thus far,  
Sarah

---

<div class="post-metadata">

**Author:** ![sarahg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a6a055/32.png) [@sarahg](https://www.ruby-forum.com/u/sarahg)\
**Post date:** [January 16, 2009, 12:07am UTC](https://www.ruby-forum.com/t/application-octet-stream-from-flash-binary-data-not-swfupload/157766/5 "2009-01-16T00:07:43Z")

</div>

On 15 Jan 2009, at 19:55, sarah wrote:

> else  
> Am I missing something from the point where the I am receiving the  
> data in the controller and getting it to attachment\_fu? It doesn’t  
> know it is application/octet-stream because it doesn’t respond to  
> content-type at the controller level.

How about:

image\_params = {:temp\_data =\> params[‘image\_uploaded\_data’],  
:filename =\> “whateveruwant”,  
:content\_type =\> “image/jpeg”  
}  
image = @visual\_note.build\_image(image\_params.merge(:created\_by =\>  
current\_user))  
if image.valid?  
#do stuff  
else  
#errors  
end

Means the data that comes from Flash will be processed and you  
manually set the other fields.

Best regards

Peter De Berdt
