Skip to content

Instantly share code, notes, and snippets.

@mpj
Created November 7, 2010 21:39
Show Gist options
  • Save mpj/666872 to your computer and use it in GitHub Desktop.
Save mpj/666872 to your computer and use it in GitHub Desktop.
Very simple example of accepting an XHR upload in Sinatra
post '/upload' do
# This accepts the XHR uploads and save the file into the script home directory
string_io = request.body # will return a StringIO
data_bytes = string_io.read # read the stream as bytes
filename = env['HTTP_X_FILE_NAME'] # This will be the actual filename of the uploaded file
# Write it to disk...
File.open(filename, 'w') {|f| f.write(data_bytes) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment