I am using PHPspreadsheet
to create xlsx file on xampp. I want to run cron jobs in wordpress that will auto export some orders and save xlsx file on xampp server. I've successfully exported a file using $writer->save('php:\\output');
but this just ask user where to download file.
I want that cron job will save file without asking user. i.e wp-content/export/$filename
Headers:
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="file.xls"');
header('Cache-Control: max-age=0');
PHP:
$objWriter = new Xlsx($objX);
ob_start();
$objWriter->save('php://output');
$xlsData = ob_get_contents();
ob_end_clean();
//returning response to javascript
$response = array(
'file_name' =>'s.xlsx',
'op' => 'ok',
'file' => "data:application/vnd.ms-excel;base64,".base64_encode($xlsData),
);
die(json_encode($response));
Javascript
jQuery.ajax({
url: ajaxurl,
type: 'POST',
dataType: 'json',
data: {
form_data :detail_info,
action: 'frontend_action_without_file' // this is going to be used inside wordpress functions.php
},
error: function(error) {
},
success: function(response) {
//console.log("Insert Success" + response.file);
}
}).done(function(data){
var $a = jQuery("<a>");
$a.attr("href",data.file);
jQuery("body").append($a);
$a.attr("download",data.file_name);
$a[0].click();
});
});