Last active
December 14, 2015 00:08
-
-
Save nufaylr/4996286 to your computer and use it in GitHub Desktop.
Push Download file
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
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