【发布时间】:2017-03-28 06:07:21
【问题描述】:
我正在尝试使用 jquery 按钮 onclick 下载 CSV 文件。我有一个<a> 标签,ID 为export,我希望它指向下载链接,我可以在其中下载我刚刚创建的CSV 文件。
// Using this jquery to send my sql query to server-side-CSV
$('#export').on('click', function() {
var sqlsend = dataTable.ajax.json().sql;
$.post('server-side-CSV.php', 'val=' + sqlsend, function(request){
//code should go here
});
});
这是我创建 CSV 文件的 php 代码
// This is my server-side-CSV file. It's creating a csv file to downloaded
<?php
require("connection.php");
$sql = $_POST['val'];
.
.more code
.
// loop over the rows, outputting them
while ($row = mysqli_fetch_assoc($rows)) fputcsv($output, $row);
fclose($output);
exit;
?>
如何下载 CSV 文件?
编辑: 终于找到答案了,
$('#export').on('click', function() {
var sqlsend = dataTable.ajax.json().sql;
window.location.href="server-side-CSV.php?val="+sqlsend;
});
【问题讨论】:
-
要么使用表单,要么打开弹出窗口,要么使用data URIs
-
如果我使用 URI,那么我如何能够将“sqlsend”变量发送到我的 php 文件?对不起,对这一切都很陌生。
-
你的
request是要下载的文件吗? -
alert(request) 给了我所有需要下载的数据。
标签: javascript php jquery html csv