百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rack-raw-upload

> 编程语言
开源

用于处理原始文件上传的机架中间件

99 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

用于处理原始文件上传的机架中间件

WARNING: you may not need this

If you want to use this because you are performing HTML5 file uploads, you may be doing it the "old way".

There is a "new", better way of doing this. It may be a better solution for your problems.

However, if this is not the case, please read on. I hope you find this middleware useful.

WARNING: there seem to be problems running this on Windows

I have received reports of this middleware not working properly on Windows environments. Unfortunately, I am not able to fix this. If you encounter such problems and are able to provide a fix with a test case, I'll be delighted to include it in a future version of this software.

Rack Raw Upload middleware

Rack::RawUpload converts raw uploads into normal multipart requests, like those in a form. Rack applications can then read these as normal (using params for example), rather than from env['rack.input'] or similar.

Assumptions

Rack::RawUpload performs this conversion when all these conditions are met:

  1. The request method is POST or PUT
  2. The mimetype is NOT one of
    • application/x-www-form-urlencoded
    • multipart/form-data
  3. The Content-Length is greater than 0

Configuration

Bog-standard Rack app

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

Ruby on Rails

Add this to your Gemfile

gem 'rack-raw-upload'

and then add the middleware in application.rb

config.middleware.use Rack::RawUpload

Usage

The upload is made into a request argument called file. In several popular frameworks, this can be accessed as params[:file]. This includes Rails and Sinatra, but may be different in other frameworks.

Optional request headers

Raw uploads, due to their own nature, can't provide additional arguments in the request. This limitation can be worked around using headers.

  • X-File-Name: specify the name of the uploaded file.
  • X-File-Upload: explicitly tells whether to perform the conversion or not. Possible values are 'true' (string), 'false' (string) or 'smart'. Default is 'smart', unless the middleware is initialized with :explicit => true, in which case default is 'false'.
  • X-Params: query string with additional arguments. On Rails or Sinatra, you can read these as params[:name_of_argument].
  • X-JSON-Params: same thing, but in the form of a JSON string.
  • X-Query-Params: deprecated. Equivalent to X-JSON-Params. Will be removed in future versions.

Additional info

A blog post on Ajax uploads. These are raw uploads and can be greatly simplified with this middleware:

  • http://blog.new-bamboo.co.uk/2010/7/30/html5-powered-ajax-file-uploads

This middleware should work with Ruby 1.8.7, 1.9.2, 1.9.3, REE, Rubinius and JRuby. Tests for all these platforms are run on the wonderful Travis-CI regularly, and the current status of these is:

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言