Skip to content

Instantly share code, notes, and snippets.

@vivisidea
Created September 20, 2012 08:37
Show Gist options
  • Save vivisidea/3754693 to your computer and use it in GitHub Desktop.
Save vivisidea/3754693 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");
});
@ThemeBing
Copy link

getparams is not defined

@vivisidea
Copy link
Author

getparams is not defined

it depends on your implementation, get whatever params for download you need.

@vivisidea
Copy link
Author

getparams is not defined

... and it was 9 years ago, people still using jQuery today?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment