php PHPExcel下载使用ajax调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27701981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHPExcel download using ajax call
提问by Sadikhasan
App::import('Vendor', 'PHPExcel/Classes/PHPExcel');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle('ReceivedMessages');
header('Content-Type: application/vnd.ms-excel');
$file_name = "kpi_form_".date("Y-m-d_H:i:s").".xls";
header("Content-Disposition: attachment; filename=$file_name");
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
When I call above code directly from the browser, the result file is downloaded. But if I make an ajax call to above code, I don't get the download prompt. I can see from console tab that the ajax call was successfully completed and a bunch of random characters is seen in the response data. I'm assuming that is the excel object.
当我直接从浏览器调用上面的代码时,会下载结果文件。但是如果我对上面的代码进行ajax调用,我就没有得到下载提示。我可以从控制台选项卡看到 ajax 调用成功完成,并且在响应数据中看到一堆随机字符。我假设那是 excel 对象。
Does anyone know how I can achieve the download excel feature using ajax? I don't want to refresh the page. When the user clicks on the "export" button, there should be an ajax call to the php file and prompt the user to download.
有谁知道我如何使用ajax实现下载excel功能?我不想刷新页面。当用户点击“导出”按钮时,应该会有php文件的ajax调用,提示用户下载。
采纳答案by Wiram Rathod
add target=_blank in your ajax success function like below
在您的 ajax 成功函数中添加 target=_blank,如下所示
success: function(){
window.open('http://YOUR_URL','_blank' );
},
otherwise you can handle smartly to open your Excel download link in new tab with jQuery trigger function or etc.
否则,您可以巧妙地使用 jQuery 触发功能等在新选项卡中打开 Excel 下载链接。
回答by Cristiano Gehring
PHP
PHP
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
ob_start();
$objWriter->save("php://output");
$xlsData = ob_get_contents();
ob_end_clean();
$response = array(
'op' => 'ok',
'file' => "data:application/vnd.ms-excel;base64,".base64_encode($xlsData)
);
die(json_encode($response));
JS
JS
$.ajax({
type:'POST',
url:"MY_URL.php",
data: {},
dataType:'json'
}).done(function(data){
var $a = $("<a>");
$a.attr("href",data.file);
$("body").append($a);
$a.attr("download","file.xls");
$a[0].click();
$a.remove();
});
回答by Rohan Kumar
You cannot download a file using ajax neither by phpexcel nor by php itself as its a security reason and almost browsers doesn't support it. But, you can try window.location in success callback like,
由于安全原因,您不能使用 ajax 下载文件,无论是 phpexcel 还是 php 本身,几乎浏览器都不支持它。但是,您可以在成功回调中尝试 window.location,例如,
var page='mydownload.php';
$.ajax({
url: page,
type: 'POST',
success: function() {
window.location = page;// you can use window.open also
}
});
Also @freakish answeredfor this type of question
@freakish 也回答了此类问题
Even, you don't need of ajax you can use hyperlink for the page like,
甚至,您不需要 ajax,您可以使用页面的超链接,例如,
<a href="mydownload.php" target="_blank" >Download</a>