Last active
December 8, 2015 03:39
-
-
Save kylekyle/f98783ad72fc9f9a3f7b to your computer and use it in GitHub Desktop.
jQuery upload to Sinatra
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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