Skip to content

Instantly share code, notes, and snippets.

@amendoncabh
Forked from vivisidea/jQuery.download.js
Created August 31, 2020 01:19
Show Gist options
  • Save amendoncabh/6942d0ddab1d6749c7de1ff180473f8c to your computer and use it in GitHub Desktop.
Save amendoncabh/6942d0ddab1d6749c7de1ff180473f8c to your computer and use it in GitHub Desktop.
download file using jquery
jQuery.download = function(url, data, method){
if( url && data ){
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();
};
};
// how to use
$("#exportbtn").click(function(){
$.download(prefix+"/export.html", getparams(), "get");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment