-
-
Save park-brian/4d705b7682a1b67ed0e48c8d23294875 to your computer and use it in GitHub Desktop.
File Download requests using jquery/POST request with psuedo ajax
This file contains 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
// Takes a URL, param name, and data string | |
// Sends to the server.. The server can respond with binary data to download | |
jQuery.download = function(url, key, data) { | |
$('<form/>') | |
.attr('action', url) | |
.attr('method', 'post') | |
.append($('<input/>') | |
.attr('type', 'hidden') | |
.attr('name', key) | |
.attr('value', data)) | |
.appendTo('body') | |
.submit() | |
.remove(); | |
}; |
This file contains 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
# A Tidbit of sinatra code to respond | |
# Assume url is a set variable | |
# Assume 'key' is the key of the value used in the javascript | |
post url do | |
data = params[:key] | |
puts request.body.read | |
headers['Content-Type'] = "application/octet-stream" | |
body(data) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment