Skip to content

Instantly share code, notes, and snippets.

@viniciussbs
Created April 10, 2011 16:23
Show Gist options
  • Save viniciussbs/912482 to your computer and use it in GitHub Desktop.
Save viniciussbs/912482 to your computer and use it in GitHub Desktop.
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>
@Fattur
Copy link

Fattur commented Jan 9, 2016

hi, it gives error on this part

File.copy(datafile[:tempfile], filename)

or

file.write(datafile[:tempfile].read)

@viniciussbs
Copy link
Author

Hi, @Fattur. This is a fork of another gist. You can check the revisions to see who is the author. But the problem could be the ruby version, because this code is from 2010.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment