我正在使用jQuery来识别对按钮的单击,然后触发对文件的调用:
window.location.href = "url";
该文件查询数据库,返回结果然后将其写入CSV文件。我已经设置了以下标题:
header('Content-Type: text/csv;');
header('Content-Disposition: attachment; filename=data.csv');
这段代码在除了Chrome浏览器以外的所有浏览器上都能正常工作,但在Chrome浏览器中会在控制台日志中返回以下错误信息:"Resource interpreted as Document but transferred with MIME type text/csv: "url""。
奇怪的是,如果我直接调用该文件,它在所有浏览器中都可以正常工作。
代码:
$fp = fopen('php://output', 'w');
header('Content-Type: text/csv;');
header('Content-Disposition: attachment; filename=data.csv');
header("Expires: 0");
header("Cache-control: private");
//Field Headers
$ncols = oci_num_fields($stid);
$headers_row = array();
for ($i = 1; $i <= $ncols; ++$i) {
$headers_row[] = oci_field_name($stid, $i);
}
while ($row = oci_fetch_array($stid, OCI_NUM+OCI_RETURN_NULLS)) {
if(!empty($row)){
if(!empty($headers_row)){
fputcsv($fp, $headers_row);
$headers_row = '';
}
fputcsv($fp, $row);
}
}
fclose($fp);
oci_close($conn);
有人有任何想法吗?
window.open(url, '_self');
。 - Andy