Skip to content

Instantly share code, notes, and snippets.

@tbuehlmann
Created August 18, 2009 11:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbuehlmann/169675 to your computer and use it in GitHub Desktop.
Save tbuehlmann/169675 to your computer and use it in GitHub Desktop.
File upload with Sinatra
require 'rubygems'
require 'sinatra'
get '/' do
@files = Dir['public/*'].map {|f| File.basename(f) }
erb :index
end
post '/upload' do
filename = params[:file][:filename]
file = params[:file][:tempfile]
File.open("public/#{filename}", 'wb') {|f| f.write file.read }
redirect '/'
end
__END__
@@ index
<h1>File upload!</h1>
<form action='/upload' method='post' enctype='multipart/form-data'>
<input type='file' name='file' /><br />
<input type='submit'/><br />
</form>
<hr />
<% @files.each do |file| %>
<a href='<%= file %>'><%= file %></a>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment