[英]Download CSV file after click button
大家早上好,当用户单击“导出CSV”按钮时,我需要强制下载csv文件,我看到了很多页面(在这里以及在google上),并尝试了很多方法,但是一切都对我有用。 我有一个带有此按钮的主页,然后单击它,将POST请求(带有Javascript和Ajax)发送到制作CSV文件并从理论上进行文件下载的另一页。 这是主页的代码:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.4/Chart.min.js"></script>
</head>
<?php
$filter="<button id='exportCSV' >Export CSV</button><br/></div>";
echo $filter
?>
<script>
$(function() {
$("#exportCSV").click(function(){
var query="select * from thcu_cabtemphpc order by timetag desc limit 300";
var table="thcu_cabtemphpc";
//alert(query+" "+table);
//alert(dataString);
$.ajax({
type: "POST",
url: "exportCSV.php",
data: {cvsExp: query, table:table},
success: function(result){
alert("Export in progress");
//$("#export").html(result);
}
});
});
});
</script>
</html>
当然,这只是一个测试,我还有另一个主页,用户可以在其中选择查询和表。 这是ExportCSV.php的代码
<html>
<body>
<?php
include('connection.php');
function array2csv($bd, $query, $id){
$sql = mysql_query($query);
$day = gmdate("d-M-Y");
$offset = +2 * 3600; // timezone offset for UTC-13
$utcTime = gmdate( 'd.m.Y H:i:s' );
$milliseconds ="".round(microtime(true) * 1000);
$val= substr($milliseconds,8,10);
//echo $val;
$valor = gmdate( 'd-m-Y_H-i-s-'.$val, time() + $offset );
$name= $day."_".$id."data_export_".$valor.".csv";
$fp = fopen($name, 'w');
while ($res=mysql_fetch_row($sql)) {
$count = 0;
foreach ($res as $val) {
if ($count == 0){
$data = gmdate("Y-m-d H:i:s", substr(($val/10000000) - 12219292800, 0,10));
$arr[$count] = $data;
$count++;
} else {
$arr[$count] = $val;
}
}
$delimiter=",";
fputcsv($fp,$arr,$delimiter);
}
//fpassthru($fp);
fclose($fp);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $name . '.csv"');
header('Content-Length: ' . filesize($name));
echo readfile($name);
}
if(isset($_POST["cvsExp"])) {
$query=$_POST["cvsExp"];
$id=$_POST["table"];
//download_send_headers("data_export_".gmdate("Y-m-d").".csv");
array2csv($bd,$query,$id);
die();
}
?></body>
</html>
我尝试这样的代码,但不起作用,我尝试替换application / octet whit text / csv,我尝试插入echo readfile($ name)或仅插入readfile($ name),但任何方法都可以。 这样做的结果是在webroot内创建一个文件,效果不好,我想让浏览器下载该文件。 非常感谢。
编辑:我尝试只执行带有下载脚本的页面,并且可以正常工作,问题是当我使用Ajax调用它时!!! 怎么可能? 错误在哪里?
我没有检查您的内部代码,但是CSV肯定不能以<html>
标记开头! 您只需要回复CSV内容,删除除<php>
所有标签。
例如:
<?php
include('connection.php');
function array2csv($bd, $query, $id){
$sql = mysql_query($query);
$day = gmdate("d-M-Y");
$offset = +2 * 3600; // timezone offset for UTC-13
$utcTime = gmdate( 'd.m.Y H:i:s' );
$milliseconds ="".round(microtime(true) * 1000);
$val= substr($milliseconds,8,10);
//echo $val;
$valor = gmdate( 'd-m-Y_H-i-s-'.$val, time() + $offset );
$name= $day."_".$id."data_export_".$valor.".csv";
$fp = fopen($name, 'w');
while ($res=mysql_fetch_row($sql)) {
$count = 0;
foreach ($res as $val) {
if ($count == 0){
$data = gmdate("Y-m-d H:i:s", substr(($val/10000000) - 12219292800, 0,10));
$arr[$count] = $data;
$count++;
} else {
$arr[$count] = $val;
}
}
$delimiter=",";
fputcsv($fp,$arr,$delimiter);
}
//fpassthru($fp);
fclose($fp);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $name . '.csv"');
header('Content-Length: ' . filesize($name));
echo readfile($name);
}
if(isset($_POST["cvsExp"])) {
$query=$_POST["cvsExp"];
$id=$_POST["table"];
//download_send_headers("data_export_".gmdate("Y-m-d").".csv");
array2csv($bd,$query,$id);
die();
}
?>
我只是删除了<html><body>
标签的标题和结尾
您指定了一种强制下载的方法(并且没有提供错误消息),因此我假设您可以通过浏览器正常访问它。
您首先需要删除该页面上的所有HTML标记。 当您echo readfile($name);
,您要下载的结果文件将看起来像这样:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.4/Chart.min.js"></script>
</head>
(YOUR CSV FILE HERE)
</body>
</html>
使用的所有header()函数都应先发送给客户端, 然后再发送到页面中的任何其他内容 ; 即使在include connection.php;
之前,它也应该位于代码的最顶层include connection.php;
线。
您无法使用Ajax下载文件( 请参阅此处 )。
相反,使用其他参数使其成为对该exportCSV.php
文件的GET
(如果需要,也可以选择POST
)请求,即:
<a href="exportCSV.php?table=table&query=name_of_the_query">Export CSV</a>
由于是文件下载,因此不会将您重定向到另一个页面,它只会开始下载文件。
如果要将php和html结合使用,请始终将逻辑和所有处理放在文件的开头,因此:
<?php
header('Content-Type: application/octet-stream');
?>
<html>
</html>
这样可以防止向浏览器发送错误的标题。 仅当没有其他头发送时, header()
函数才有效。
请记住,必须先通过常规HTML标记,文件中的空白行或从PHP发送任何实际输出,然后调用header()。 (资源)
我需要提及的是,您永远不要使用POST
/ GET
参数发送SQL查询! 而是发送一些信息,例如查询名称和查询参数,然后您可以使用它们来正确,安全地构建SQL查询,即:
function array2scv($queryName, $queryParameters) {
$queries = [
'example' = 'SELECT * FROM user WHERE id = ?',
];
$stmt = $mysqli->prepare($queries[$queryName]);
$stmp->bind_param('i', $queryParameters['id']);
$stmp->execute();
...
}
然后在该函数中return readfile($name)
而不是回显它。 然后使用exit()
代替die()
。 (资源)
这是一个简单脚本的工作示例:
<?php
// example csv
$url = 'http://samplecsvs.s3.amazonaws.com/Sacramentorealestatetransactions.csv';
function download_file()
{
// getting content to use in the example
$content = file_get_contents($url);
$filename = '/tmp/sample.csv';
// creating temp file
$handle = fopen($filename, 'w+');
fwrite($handle, $content);
fclose($handle);
// setting headers
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Content-Length: ' . filesize($filename));
return readfile($filename);
}
if (isset($_GET['export']) && $_GET['export'] === '1') {
download_file();
exit();
}
?>
<html>
<body>
<a href="?export=1">Export</a>
</body>
</html>
我希望这会有所帮助。
看到这是我用于动态下载csv的简单代码。
if(isset($_POST['somebutton']))
{
$filename = "somefilename.csv";
$fp = fopen('php://output', 'w');
$query = "select * from thcu_cabtemphpc order by timetag desc limit 300";
$result = mysql_query($query);
for($i=0;$i<=7;$i++)
{
$header[] = mysql_field_name($result, $i);
}
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
fputcsv($fp, $header);
$query = "select * from thcu_cabtemphpc order by timetag desc limit 300";
$result12= mysql_query($query);
while($row12 = mysql_fetch_row($result12)) {
fputcsv($fp, $row12);
}
exit;
}
这是我用于下载简单Excel的代码。 及其正常工作的Fine.put此php到任何地方,您就可以下载csv。
首先,我要感谢所有人的大量回答,今天我找到了一个解决方案,对我来说,它可以正常工作,我在主页上添加以下内容:
$(function() {
$('#CSV').click(function(){
$('#wait-animation').show();
var where="<?php Print($where) ?>";
var table="<?php Print($id) ?>";
//var table="thcu_cabtemphpc";
alert("Export in progress");
alert(query);
document.location.href = 'exportCSV.php?where='+where+'&table='+table;
$('#wait-animation').hide();
});
});
并在导出页面中:
<?php
include('connection.php');
header("Last-Modified: " . @gmdate("D, d M Y H:i:s",$_GET['timestamp']) . " GMT");
header("Content-type: text/x-csv");
// If the file is NOT requested via AJAX, force-download
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
header("Content-Disposition: attachment; filename=search_results.csv");
}
$table=$_GET['table'];
$where=$_GET['where'];
$day = gmdate("d-M-Y");
$offset = +2 * 3600; // timezone offset for UTC-13
$utcTime = gmdate( 'd.m.Y H:i:s' );
$milliseconds ="".round(microtime(true) * 1000);
$val= substr($milliseconds,8,10);
$valor = gmdate( 'd-m-Y_H-i-s-'.$val, time() + $offset );
$filename= $day."_".$table."data_export_".$valor.".csv";
$fp = fopen('php://output', 'w');
$query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='monitoring' AND TABLE_NAME='".$table."'";
$result = mysql_query($query);
while ($row = mysql_fetch_row($result)) {
$header[] = $row[0];
}
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
fputcsv($fp, $header);
$result = mysql_query("select * from ".$id." where ".$where);
while($row = mysql_fetch_row($result)) {
fputcsv($fp, $row);
}
return $filename;
exit;
?>
非常感谢。 祝你今天愉快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.