Skip to content

Instantly share code, notes, and snippets.

@pfeilbr
Created March 19, 2010 11:58
Show Gist options
  • Save pfeilbr/337450 to your computer and use it in GitHub Desktop.
Save pfeilbr/337450 to your computer and use it in GitHub Desktop.
ruby sinatra file upload example
require 'rubygems'
require 'sinatra/base'
class MyApp < Sinatra::Base
get '/' do
'myapp2 Hello world!'
end
get '/hello' do
html = "<pre>"
env.each do |k,v|
html += k + " = " + v.to_s + "<br/>"
end
html + "</pre>"
end
post '/form' do
html = "<pre>"
env.each do |k,v|
html += k + " = " + v.to_s + "<br/>"
end
html + "</pre>"
params.to_s
tmpfile = params[:file][:tempfile]
tmpfile.read
end
get '/form' do
%{
<html>
<body>
<form action="form" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" />
</form>
</body>
</html>
}
end
end
MyApp.run! :host => 'localhost', :port => 8082, :server => 'thin'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment