Created
February 8, 2009 14:34
-
-
Save vangberg/60396 to your computer and use it in GitHub Desktop.
file upload w/ sinatra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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