Using ajax to download files is a convenient, fast, fashionable, and challenging function. First, obtain the url and data, and then assemble the parameters into form input, and then send the request using the request, that is, to dynamically render the form and then delete it after submitting the form. In this example, the file download function is implemented. If you are interested, you can click here.
The Code is as follows:
// Download an Ajax File
JQuery. download = function (url, data, method ){
// Obtain url and data
If (url & data ){
// Data is string or array/object
Data = typeof data = 'string '? Data: jQuery. param (data );
// Assemble parameters into form input
Var inputs = '';
JQuery. each (data. split ('&'), function (){
Var pair = this. split ('= ');
Inputs + ='';
});
// Request to send a request
JQuery ('')
. AppendTo ('body'). submit (). remove ();
};
};
* ** This is the dynamic rendering form. after submitting the form, delete it. ***
Call an instance
The Code is as follows:
$. Download ('exceldownload. do ', 'Find = commoncode', 'post ');