Skip to content

Instantly share code, notes, and snippets.

@kevinold
Created August 8, 2011 15:58
Show Gist options
  • Select an option

  • Save kevinold/1132028 to your computer and use it in GitHub Desktop.

Select an option

Save kevinold/1132028 to your computer and use it in GitHub Desktop.
Upload file in Sinatra
require 'rubygems'
require 'sinatra'
require 'fileutils'
# upload with:
# curl -v -F "data=@/path/to/filename" http://localhost:4567/user/filename
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.open(filename, 'wb') do |file|
file.write(datafile[:tempfile].read)
end
"wrote to #{filename}\n"
end
@locisvv
Copy link

locisvv commented Jun 29, 2013

Hi)) How to upload multiple files?

@polarlights
Copy link

It's a little different between Sinatra and Rails. In Ruby On Rails, we would access the tempfile by datafile.tempfile; on the other hand, we should use a hash to access it in Sinatra.

@clausd
Copy link

clausd commented Jun 16, 2016

There's a naming inconsistency - the file is in params[:file] with the markup you have

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