public
Last active

  • Download Gist
trivial_file_upload_service.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
require 'rubygems'
require 'sinatra'
 
require 'fileutils'
 
# upload with:
# curl -v -F "data=@/path/to/filename" http://localhost:4567/user/filename
# or just go to http://localhost:4567/user/filename with a browser
 
get '/:name/:filename' do
erb :form
end
 
post '/:name/:filename' do
userdir = File.join("files", params[:name])
FileUtils.mkdir_p(userdir)
filename = File.join(userdir, params[:filename])
datafile = params[:data]
# "#{datafile[:tempfile].inspect}\n"
File.copy(datafile[:tempfile], filename)
#File.open(filename, 'wb') do |file|
# file.write(datafile[:tempfile].read)
#end
"wrote to #{filename}\n"
end
 
 
use_in_file_templates!
 
__END__
 
@@ form
<form action="" method="post" enctype="multipart/form-data">
<p>
<label for="file">File:</label>
<input type="file" name="file">
</p>
 
<p>
<input name="commit" type="submit" value="Upload" />
</p>
</form>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.