Skip to content

Instantly share code, notes, and snippets.

@vangberg
Created February 8, 2009 14:34
Show Gist options
  • Save vangberg/60396 to your computer and use it in GitHub Desktop.
Save vangberg/60396 to your computer and use it in GitHub Desktop.
file upload w/ 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}", 'w') {|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