Everyone knows that Ajax can't download files directly, so it's usually a hyperlink to download a file.
But when it comes to the need to send a lot of data to the server to download again, the form of hyperlinks is a bit too reluctant.
The following is a tool method (which relies on jquery) to download a file by sending multiple data, with the following code:
/*=================== Download file * options:{* URL: ',//* Data:{name:value},//data to be sent * method: ' Post ' *}*/varDownLoadFile =function(options) {varConfig = $.extend (true, {method: ' Post '}, Options); var$iframe = $ (' <iframe id= "down-file-iframe"/> '); var$form = $ (' <form target= "down-file-iframe" method= "' + Config.method + '"/> '); $form. attr (' Action ', Config.url); for(varKeyinchconfig.data) {$form. Append (' <input type= ' hidden "name=" ' + key + ' "value=" ' + config.data[key ' + '/> '); } $iframe. Append ($form); $ (document.body). Append ($iframe); $form [0].submit (); $iframe. Remove ();
Very simple, like URL and data can be
Demo:download ({
URL: ' http://www.baidu.com ... ',//URL requested
DATA:{SC: ' xxx '}//data to send
});
JS send Post request download file