文件上传的时候,默认的是把图片分割成多份然后打包发送到 server 的内存中,待所有包都过来之后再写入硬盘上,那我怎么用 ruby 来读取内存中的每次发来的包数据呢?
#1 楼 @jimrokliu 我在写一上传图片的 api,post 一 file 到我这边,想发一个包接一个包,减少内存占用.redis 没试过,需要的时候可能会更长,想先用 ruby 这样实现一下。
#3 楼 @jimrokliu 那先不考虑内存问题。这样我想边接包边进行处理,比等它接收完成之后一次性处理要快吧,另外它接完之后放在硬盘上,我处理的时候就要再次与硬盘交互。不如接一个处理一个来的快吧。
你可以使用 http://www.sinatrarb.com/intro.html
get '/post.*' do
t = %w[text/css text/html application/javascript]
request.accept # ['text/html', '*/*']
request.accept? 'text/xml' # true
request.preferred_type(t) # 'text/html'
request.body # request body sent by the client (see below)
request.scheme # "http"
request.script_name # "/example"
request.path_info # "/foo"
request.port # 80
request.request_method # "GET"
request.query_string # ""
request.content_length # length of request.body
request.media_type # media type of request.body
request.host # "example.com"
request.get? # true (similar methods for other verbs)
request.form_data? # false
request["some_param"] # value of some_param parameter. [] is a shortcut to the params hash.
request.referrer # the referrer of the client or '/'
request.user_agent # user agent (used by :agent condition)
request.cookies # hash of browser cookies
request.xhr? # is this an ajax request?
request.url # "http://example.com/example/foo"
request.path # "/example/foo"
request.ip # client IP address
request.secure? # false (would be true over ssl)
request.forwarded? # true (if running behind a reverse proxy)
request.env # raw env hash handed in by Rack
end
数据都在 request.body
自己读 socket,就能控制 buffer 了,读一点写一点就可以了 大概这样:http://stackoverflow.com/questions/13533382/how-to-read-from-a-tcpserver-socket-in-ruby-using-read-readpartial-and-read-non