Skip to content

Instantly share code, notes, and snippets.

@Brimizer
Created June 16, 2014 18:40
Show Gist options
  • Save Brimizer/f1e7e36fc068fa1b66c1 to your computer and use it in GitHub Desktop.
Save Brimizer/f1e7e36fc068fa1b66c1 to your computer and use it in GitHub Desktop.
iOS POST file to Sinatra
require 'sinatra'
require 'fileutils'
require 'json'
post '/:filename' do
content_type :json
puts "Opening directory"
dirname = File.expand_path("~/my_dir")
unless File.directory?(dirname)
puts "Creating directory"
FileUtils.mkdir_p(dirname)
end
filename = File.join(dirname, params[:filename])
postedfile = params[:file] # The 'file' paramter is passed in with the request
puts "Opening new file"
File.open(filename, 'wb') do |file|
puts "Writing file to disk"
file.write(postedfile[:tempfile].read)
end
{ :success => 'true', :file => "#{filename}" }.to_json
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment