gpt4 book ai didi

javascript - 可通过 Ajax 下载 CSV 文件

转载 作者:行者123 更新时间:2023-11-28 00:36:27 24 4
gpt4 key购买 nike

如果我成功使用 Ajax,我无法弄清楚如何获取可下载的 csv 文件。我将链接 js ajax 脚本以及 Ajax 函数中调用的 php 文件。感谢您的帮助。我将返回 Ajax 函数的 Success 函数。我只是不知道如何使我的数据作为可下载的 csv 文件返回。

JS函数:

function popupClick2 (){
var popupObj2 = {};
var c = '0';
var p = '0';
var i = '0';
if (document.getElementById('checkboxC').checked){c = '1'}
if (document.getElementById('checkboxP').checked){p = '1'}
if (document.getElementById('checkboxI').checked){i = '1'}
popupObj2["checkboxC"] = c;
popupObj2["checkboxP"] = p;
popupObj2["checkboxI"] = i;
popupObj2["rangeD"] = $('#rangeD').val();
popupObj2["year"] = $('#year').val();
popupObj2["popupObj"] = '2';
$.ajax({
type: "POST",
dataType: "text",
url: "popupAjax.php",
data: popupObj2,
cache: false,
success: function(data)
{
alert("Success");
//I would like to have the csv file downloadable here.
},
error: function(jqXHR, textStatus, errorThrown)
{
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
}
});
closePopup2();
}

PHP(popupAjax.php)

<?php
$weekEnding = '';
$PHSN = '';

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=eWFO-Report.csv');

$output = fopen('php://output', 'w');

fputcsv($output, array('Week Ending', 'WN', 'Project Title', 'Project Contact',
'Org No', 'PHSN', 'No', 'Verified By', 'Date Verified',
'Comments', 'Notes'));

/*** connect to SQL DB ***/
$dbe = get_db_connection('db');
$dbe->connect();
/*** connect or Oracle DB ***/
$db = oci_connect('query','pw','server:1521/world');
if (!$db){
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$query = "SELECT * FROM db.dbstuff WHERE (STATUS = 'ACTIVE' OR STATUS = 'CLOSED') AND NUMBER <> ' '";

$runQuery = oci_parse($db, $query);
oci_execute($runQuery);

while($row = oci_fetch_array($runQuery, OCI_ASSOC+OCI_RETURN_NULLS))
{
$WFON = $row['NUMBER']."-".$row['ANUMBER'];

$querySQLDB = "SELECT [Verified_By], [Comments], [Notes], [Date_Verified]
FROM dbo.Information
WHERE dbo.Information.Key_ID = '$WFON'
ORDER BY dbo.Information.ID DESC";
$dbe->query($querySQLDB);
$sqlData = $dbe->fetch();

$dateNoTime = str_replace("12:00:00:000AM"," ",$sqlData['Date_Verified']);

fputcsv($output, array($weekEnding, $WFON, $row['TITLE'], $row['NAME'],
$row['ORG'], $PHSNumber, $sqlData['Verified_By'], $dateNoTime,
$sqlData['Comments'], $sqlData['Notes']));

}
echo $output;
?>

最佳答案

您可以在 popupClick2 函数中动态添加表单,例如:

function popupClick2 (){

...

($('<form/>', {
'id': 'tmpCsvForm',
'action': "popupAjax.php",
'method': 'post'
}).append($('<input />', {
'type': 'hidden',
'name': 'data',
'value': popupObj2
}))).appendTo('body');

$('form#tmpCsvForm').submit().remove();
}

关于javascript - 可通过 Ajax 下载 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28442781/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com