# Rails can't handle binary file upload?

**URL:** <https://www.ruby-forum.com/t/rails-cant-handle-binary-file-upload/71566>\
**Category:** Rails\
**Created:** [October 10, 2006, 12:15am UTC](https://www.ruby-forum.com/t/rails-cant-handle-binary-file-upload/71566 "2006-10-10T00:15:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gyoung-Yoon\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@Gyoung-Yoon\_N](https://www.ruby-forum.com/u/Gyoung-Yoon_N)\
**Post date:** [October 10, 2006, 12:15am UTC](https://www.ruby-forum.com/t/rails-cant-handle-binary-file-upload/71566/1 "2006-10-10T00:15:03Z")

</div>

Hi,

I am trying to upload local file to a rails-app through HTTP  
without multipart encoding. Doing so, I expect to get some  
performance boost on transferring large files. I am stucked  
Rails can’t handle binary file upload, while it’s OK with plain  
text files. Let me show a sample application skeleton:

config/routes.rb:

map.connect ‘upload’, :controller =\> ‘application’, :action =\> ‘upload’

app/controllers/application.rb:

class ApplicationController \< ActionController::Base  
def upload  
render :text =\> request.raw\_post  
end  
end

Uploading plain file succeeds as following:

$ curl -v -T 42.txt [http://localhost/upload](http://localhost/upload)

- About to connect() to localhost port 80
- Trying 127.0.0.1… \* connected
- Connected to localhost (127.0.0.1) port 80

> PUT /upload HTTP/1.1  
> User-Agent: curl/7.13.1 (powerpc-apple-darwin8.0) libcurl/7.13.1  
> OpenSSL/0.9.7i zlib/1.2.3  
> Host: localhost  
> Pragma: no-cache  
> Accept: _/_  
> Content-Length: 3  
> Expect: 100-continue

\< HTTP/1.1 100 Continue  
\< HTTP/1.1 200 OK  
\< Date: Mon, 25 Sep 2006 18:51:11 GMT  
\< Server: Mongrel 0.3.13.4

- Added cookie \_session\_id=“b64a0dde1c857bd0d94ea95869f90294” for  
domain localhost, path /, expire 0  
\< Set-Cookie: \_session\_id=b64a0dde1c857bd0d94ea95869f90294;  
domain=localhost; path=/  
\< Status: 200 OK  
\< Cache-Control: no-cache  
\< Content-Type: text/html; charset=UTF-8  
\< Content-Length: 3  
42
- Connection #0 to host localhost left intact
- Closing connection #0

Uploading binary file fails like this:

$ curl -v -T photo1.jpg [http://localhost/upload](http://localhost/upload)

- About to connect() to localhost port 80
- Trying 127.0.0.1… \* connected
- Connected to localhost (127.0.0.1) port 80

> PUT /upload HTTP/1.1  
> User-Agent: curl/7.13.1 (powerpc-apple-darwin8.0) libcurl/7.13.1  
> OpenSSL/0.9.7i zlib/1.2.3  
> Host: localhost  
> Pragma: no-cache  
> Accept: _/_  
> Content-Length: 60591  
> Expect: 100-continue

\< HTTP/1.1 100 Continue  
\< HTTP/1.1 200 OK  
\< Date: Mon, 25 Sep 2006 18:52:15 GMT  
\< Content-Type: text/html  
\< Content-Length: 380  
Status: 500 Internal Server Error  
Content-Type: text/html

# Application error (Apache)

Change this error message for exceptions thrown outside of an action (like in Dispatcher setups or broken Ruby code) in public/500.html

\* Connection #0 to host localhost left intact \* Closing connection #0

I’ve written small cgi application to prove it might perform better,  
and it doesn’t show any problem. So I would like to apply my idea  
to Rails application too. Comments are appreciated.

–  
[http://nohmad.sub-port.net](http://nohmad.sub-port.net)

---

<div class="post-metadata">

**Author:** ![Gyoung-Yoon\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@Gyoung-Yoon\_N](https://www.ruby-forum.com/u/Gyoung-Yoon_N)\
**Post date:** [October 10, 2006, 2:19am UTC](https://www.ruby-forum.com/t/rails-cant-handle-binary-file-upload/71566/2 "2006-10-10T02:19:14Z")

</div>

On Tue, 10 Oct 2006 07:13:01 +0900  
“Gyoung-Yoon N.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I am trying to upload local file to a rails-app through HTTP  
> without multipart encoding. Doing so, I expect to get some  
> performance boost on transferring large files. I am stucked  
> Rails can’t handle binary file upload, while it’s OK with plain  
> text files. Let me show a sample application skeleton:

Rails probably can’t, but you could go straight to source and use a  
Mongrel handler. Here’s the shell to get you started:

class BinaryUploadHandler \< Mongrel::HttpHandler

def process(req, resp)  
# req.body at this point is a IO object, either StringIO or Tmpfile  
end  
end

uri “/uploadstuff”, :handler =\> BinaryUploadHandler.new

Put that in a mongrel.conf and start mongrel with:

mongrel\_rails start -e production -S mongrel.conf

Then you’re job is to fill in the process(req,resp) method to take the  
req.body and either move it or copy it to where you want. You also have  
access to headers and other goodies and can do responses.

Check out the code to mongrel\_upload\_progress (gem install  
mongrel\_upload\_progress) for a good example of doing this and also  
tracking the upload. You could probably just modify that gem and be  
done with 80% of the work already.

–  
Zed A. Shaw, MUDCRAP-CE Master Black Belt Sifu

> **[Zed A. shaw](https://zedshaw.com/)**
>
> Personal essays and writings by Zed A. Shaw.

[http://mongrel.rubyforge.org/](http://mongrel.rubyforge.org/)  
[http://www.lingr.com/room/3yXhqKbfPy8](http://www.lingr.com/room/3yXhqKbfPy8) – Come get help.

---

<div class="post-metadata">

**Author:** ![Gyoung-Yoon\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@Gyoung-Yoon\_N](https://www.ruby-forum.com/u/Gyoung-Yoon_N)\
**Post date:** [October 10, 2006, 4:59pm UTC](https://www.ruby-forum.com/t/rails-cant-handle-binary-file-upload/71566/3 "2006-10-10T16:59:29Z")

</div>

Hi, Zed.

I had already implemented with standalone mongrel-powered server  
which rely on a number of existing rails models. However, I’d like to  
handle file-upload on my rails-app, because well… here is rails list!?  
🙂

Another question: mongrel can handle 100 Expect request?  
FYI, here’s my sample CGI application:

if ENV[‘HTTP\_EXPECT’] == ‘100-Continue’  
puts “Status: HTTP 100 Continue”  
content = STDIN.read(ENV[‘CONTENT\_LENGTH’].to\_i)  
File.open(‘upload.data’, ‘w’).write(content)  
end  
puts “Content-Type: text/html”  
puts  
puts “Done.”

2006/10/10, Zed A. Shaw [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > Rails can’t handle binary file upload, while it’s OK with plain  
> > end
> 
> –  
> Zed A. Shaw, MUDCRAP-CE Master Black Belt Sifu  
> [http://www.zedshaw.com/](http://www.zedshaw.com/)  
> [http://mongrel.rubyforge.org/](http://mongrel.rubyforge.org/)  
> [http://www.lingr.com/room/3yXhqKbfPy8](http://www.lingr.com/room/3yXhqKbfPy8) – Come get help.
> 
> >

–  
[http://nohmad.sub-port.net](http://nohmad.sub-port.net)
