Skip to content

Instantly share code, notes, and snippets.

@nufaylr
Last active December 14, 2015 00:08
Show Gist options
  • Save nufaylr/4996286 to your computer and use it in GitHub Desktop.
Save nufaylr/4996286 to your computer and use it in GitHub Desktop.
Push Download file
jQuery.download = function(url, data, method){
//url and data options required
if( url && data ){
//data can be string of parameters or array/object
data = typeof data == 'string' ? data : jQuery.param(data);
//split params into form inputs
var inputs = '';
jQuery.each(data.split('&'), function(){
var pair = this.split('=');
inputs+='<input type="hidden" name="'+ pair[0] +'" value="'+ pair[1] +'" />';
});
//send request
jQuery('<form action="'+ url +'" method="'+ (method||'post') +'">'+inputs+'</form>')
.appendTo('body').submit().remove();
};
};
/*
Usage : $("#downloadLink").click(function() {
var getVal = $("#txt").val();
$.download('http://localhost/localhost.test/api/','text='+getVal+'');
return false;
});
Original snippet from : http://www.filamentgroup.com/lab/jquery_plugin_for_requesting_ajax_like_file_downloads/
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment