【发布时间】:2015-10-28 16:34:30
【问题描述】:
我正在尝试使用 ajax 构建一个“导出到 CSV”系统,所以 这是我的ajax调用
$(document).on('click', '#export-csv', function(){
$.ajax({
url: export_url,
type: 'post',
data:{ validations: validations },
dataType: 'json',
cache: false,
timeout: 1000000,
success: function (data) {
var resp = data.response;
console.log(resp);
},
error: function (error) {
console.log(error);
}
});
});
这是我将数组转换为 csv 的 php 代码
public function exportCsvAction()
{
$request = $this->getRequest();
if ($request->isPost()) {
$input = $request->getPost();
$response = $this->getResponse();
$file = fopen('php://output', 'w');
foreach ($input['validations'] as $line) {
fputcsv($file, $line, chr(9), '"');
}
fclose($file);
header('Content-type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="validations-' . time() . '.csv"');
return $response->setContent(\Zend\Json\Json::encode(array('response' => true)));
}
return $this->getResponse()->setStatusCode(400);
}
一切正常。我的 ajax 返回成功,但现在我被卡住了......
ajax调用成功后如何下载文件?下一步是什么?
我已经进行了研究,但没有发现任何相关和/或有用的信息。
提前致谢。
【问题讨论】:
标签: php jquery ajax zend-framework2