Multipart Response in Ruby/Rack

Multipart response in Ruby/Rack

You can probably use the out.flush method for this:

class TestController < ApplicationController
def index
render :text => lambda { |resp, out|
out.puts 'start'
out.flush
10.times do
out.puts '.'
out.flush
sleep 1
end
out.puts 'done'
}
end
end

However, keep in mind that if you're using Mongrel to serve your Ruby code (as many people using RoR do), you won't be able to stream at all.

What is Rack::Utils.multipart_part_limit within Rails and what function does it perform?

Telling it short, this value limits the amount of simultaneously opened files for multipart requests.
To understand better what is multipart, you can see this question.

The reason for this limitation is an ability to better adjust your app for your server. If you have too many files opened at a time, your system can run out of handles. If you are not worried of this you can set this value to 0 in your initializer like this Rack::Utils.multipart_part_limit = 0

What's the fastest way for a true sinatra(ruby/rack) after_filter?

Modified run_later port to rails to do the trick the file is available here:

http://github.com/pmamediagroup/sinatra_run_later/tree/master

Is it possible to sending a streaming response from a simple Ruby / Rack app?

Sounds like you need a chunked transfer. There is a Rack::Chunked middleware for it too within Rack.

Why is rack response body an array not a string?

I think rack originated on python's wsgi. Here is the explanation for python:
http://www.python.org/dev/peps/pep-3333/#buffering-and-streaming

Ruby Sinatra with multi part post request

I think (although I can’t reproduce exactly what you’re seeing) this is due to the Thin server not handling chunked transfer encoded requests. Node is breaking up the file into several parts and trying to send each chunk separately. The strange string (13b2b) is the hex value of the size of the first chunk (although that doesn’t actually match the data in your Gist) and it is cut off because the client is expecting to send the rest later.

The Node server works because it properly handles chunked requests, and the other (malformed) requests that Sinatra is seeing are the following chunks.

You can try using another server in your Sinatra app to see if it works. Webrick seems to be okay:

set :server, 'webrick'

Multiple file uploads in Sinatra

The only thing that puts me off here is that you don't use the POST method – maybe your issue has to do with that. Anyway, the following code works perfectly for me. I hope this will give you a hint how to fix your code.

require 'sinatra'

get '/' do
<<-HTML
<html>
<head><title>Multi file upload</title></head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="images[]" multiple />
<input type="submit" />
</form>
</body>
</html>
HTML
end

post '/upload' do
content_type :text

res = "I received the following files:\n"
res << params['images'].map{|f| f[:filename] }.join("\n")
res
end

mechanize and Ruby multipart/form-data - content transfer encoding

I have managed to identify the culprit here. As my development machine is Windows-based, this seems to have been an issue with mechanize (or one of its dependencies) and Windows. By specifying the b (binary) part in the second argument of File.new, the problem went away on its own. tl;dr: here's how the working code fragment now looks like:

agent.post(base_uri + RESOURCES_PATH, {
:force_create => true,
:file => File.new(file, 'rb') # <-- changed
})


Related Topics



Leave a reply



Submit