Skip to content

Instantly share code, notes, and snippets.

@donhenton
Forked from DavidMah/filedownloader.js
Created May 7, 2016 13:23
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 donhenton/7058860b3e39c4c9ae0888cfe5e2bdaa to your computer and use it in GitHub Desktop.
Save donhenton/7058860b3e39c4c9ae0888cfe5e2bdaa to your computer and use it in GitHub Desktop.
File Download requests using jquery/POST request with psuedo ajax
// 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){
// Build a form
var form = $('<form></form>').attr('action', url).attr('method', 'post');
// Add the one key/value
form.append($("<input></input>").attr('type', 'hidden').attr('name', key).attr('value', data));
//send request
form.appendTo('body').submit().remove();
};
# 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