Rack::RawUpload converts raw file uploads into normal form input, so Rack applications can read these as normal (using params
for example), rather than from env['rack.input']
or similar.
Rack::RawUpload know that a request is such an upload when the mimetype is not one of the following:
- application/x-www-form-urlencoded
- multipart/form-data
Additionally, it can be told explicitly to perform the conversion, using the header X-File-Upload
. See below for details.
Rack::RawUpload expects that requests will:
- be POST or PUT requests
- set the mimetype
application/octet-stream
The simpler case. Add these lines in your config.ru
, where appropriate:
require 'rack/raw_upload'
use Rack::RawUpload
If you want to limit the conversion to a few known paths, do:
require 'rack/raw_upload'
use Rack::RawUpload, :paths => ['/upload/path', '/alternative/path.*']
You can also make it so that the conversion only happens when explicitly required by the client using a header. This would be X-File-Upload: true
to make the conversion regardless of the content type. A value of X-File-Upload: smart
would ask for the normal detection to be performed. For this, use the following setting:
use Rack::RawUpload, :explicit => true
Add this to your Gemfile
gem 'rack-raw-upload'
and then add the middleware in application.rb
config.middleware.use 'Rack::RawUpload'
Raw uploads, due to their own nature, don't include the name of the file being uploaded. You can work around this limitation by specifying the filename as an HTTP header.
When present, Rack::RawUpload will assume that the header X-File-Name
will contain the filename.
Again, the nature of raw uploads prevents us from sending additional parameters along with the file. As a workaround, you can specify there as a header too. They will be made available as normal parameters.
When present, Rack::RawUpload will assume that the header X-Query-Params
contains these additional parameters. The values are expected to be in the form of a JSON hash.
A blog post on HTML5 uploads, which are raw uploads, and can be greatly simplified with this middleware:
This middleware should work with Ruby 1.8.7, 1.9.2, REE, Rubinius and JRuby. Tests for all these platforms are run on the wonderful Travis-CI regularly, and the current status of these is: