可以通过创建的php中的php下载xlsx文件
在public/js文件中,我请求$ ajax,然后仅响应字符串而不是文件。
如果客户端使用AJAX发送请求,则服务器使用字符串而不是XLSX文件响应。
这是请求部分:
$("#downloadXLS").on('click', function () {
$.ajax({
type: 'get',
url: appRoot + "transactions/downloadXLS/",
success: function () {
},
error: function () {
}
});
});
这里是php响应函数:
public function downloadXLS()
{
// Filter the excel data
function filterData(&$str)
{
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if (strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}
// Excel file name for download
$fileName = "transaction-data_" . date('Y-m-d') . ".xls";
// Column names
$fields = array('No.', 'Pay Date', 'Company');
// Display column names as first row
$excelData = implode("\t", array_values($fields)) . "\n";
// Headers for download
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"$fileName\"");
$allTranscations = $this->transaction->getAll();
if (is_array($allTranscations)) {
// Output each row of the data
foreach ($allTranscations as $key => &$trans) {
$lineData = array($key + 1, $trans->pay_time, $trans->company);
array_walk($lineData, 'filterData');
$excelData .= implode("\t", array_values($lineData)) . "\n";
}
} else {
$excelData .= 'No records found...' . "\n";
}
// Render excel data
echo $excelData;
exit();
}
In public/js file, I request $ajax, and get responding just string, not file.
If the client sends a request using ajax, the server responds with a string instead of xlsx file.
Here is request part:
$("#downloadXLS").on('click', function () {
$.ajax({
type: 'get',
url: appRoot + "transactions/downloadXLS/",
success: function () {
},
error: function () {
}
});
});
And here is php responding function:
public function downloadXLS()
{
// Filter the excel data
function filterData(&$str)
{
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if (strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}
// Excel file name for download
$fileName = "transaction-data_" . date('Y-m-d') . ".xls";
// Column names
$fields = array('No.', 'Pay Date', 'Company');
// Display column names as first row
$excelData = implode("\t", array_values($fields)) . "\n";
// Headers for download
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"$fileName\"");
$allTranscations = $this->transaction->getAll();
if (is_array($allTranscations)) {
// Output each row of the data
foreach ($allTranscations as $key => &$trans) {
$lineData = array($key + 1, $trans->pay_time, $trans->company);
array_walk($lineData, 'filterData');
$excelData .= implode("\t", array_values($lineData)) . "\n";
}
} else {
$excelData .= 'No records found...' . "\n";
}
// Render excel data
echo $excelData;
exit();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法通过AJAX请求下载文件。
尝试位置。
You can't download file via ajax request.
Try location.href=transactions/downloadXLS.