Skip to content

Instantly share code, notes, and snippets.

@kylekyle
Last active December 8, 2015 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylekyle/f98783ad72fc9f9a3f7b to your computer and use it in GitHub Desktop.
Save kylekyle/f98783ad72fc9f9a3f7b to your computer and use it in GitHub Desktop.
jQuery upload to Sinatra
require 'slim'
require 'sinatra'
template :upload do
<<-END
head
script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"
javascript:
$(document).ready(function() {
$('#upload :button').click(function(){
$.ajax({
url: '/',
type: 'POST',
success: function(response) { alert("Success! " + response) },
error: function(response) { alert("Nope. " + response.responseText) },
data: new FormData($('#upload')[0]),
cache: false,
contentType: false,
processData: false
});
});
});
body
form id='upload' enctype='multipart/form-data'
input type='file' name='filename'
input type='button' value='Upload'
END
end
get '/' do
slim :upload
end
post '/' do
[
[200, "You uploaded #{params['filename'][:tempfile].size} bytes!"],
[400, "You do not smell good enough to upload to this site! Shower and try again."],
[400, "I can tell you've been dabbling in Python. No uploads until you've repented."],
[400, "I can smell the fear on you. Cowards cannot upload to this site!"]
].sample
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment