ajax — CSV не создается и не загружается в браузере

У меня есть массив данных, которые я хочу создать в формате CSV и хочу загрузить его в своем браузере. Я создаю его с помощью вызова Ajax.

Вот мой код Ajax:

<script type="text/javascript" language="javascript">
$(document).ready(function(){
$("#exportBtn").click(function(){
$.ajax({
url: "<?php echo WEB_URL; ?>process/adminProcess.php?page=exportFile",
async: true,
type: "POST",
data: $('#reportForm').serialize(),
beforeSend: function(){
$("#file").html("Generating Your File");
}
})
});
});
</script>

Вот код моей страницы, на который идет этот звонок:

if(isset($_GET['page']) && $_GET['page']=="exportFile"){

$data = $objadminViewFunctions->exportFile();

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=data.csv");
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");

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

fputcsv($output, array('Serial No.', 'Report Number', 'Pallet Id', 'Type', 'Consignee', 'Damage', 'Damage Description', 'Label', 'Vessel', 'Location', 'Suryeyor'));

foreach ($data as $sss) {
fputcsv($output, $sss);
}
fclose($outstream);

}

Вот данные в $ data, которые я хочу ввести в файл CSV:

Array
(
[0] => stdClass Object
(
[de_ID] => 1
[report_ID] => 1
[pallet_ID] => 4788
[type] => Apple
[consignee] => Dandrea
[damage] => Stow Damage
[damage_desc] => TT Damage
[label] => Valdovinos
[variety] => Flame Seedless
[category_code] => Empty and/or Missing Cartons
[pieces] => 2
[hold] => 2
[deck] => C
[dDate] => 2014-12-08
[e_ID] => 1
[report_number] => 123
[vessel] => 321
[location] => Phoenix 602
[eDate] => 2014-12-01
[suryeyor] => Mohsin
)

[1] => stdClass Object
(
[de_ID] => 2
[report_ID] => 3
[pallet_ID] => 8696
[type] => Peach
[consignee] => Del Monte
[damage] => Breakout Damage
[damage_desc] => TT Damage
[label] => Agricom
[variety] => Mango
[category_code] => Damage to contents of cartons
[pieces] => 4
[hold] => 2
[deck] => P
[dDate] => 2014-12-08
[e_ID] => 3
[report_number] => 526
[vessel] => 748
[location] => Atlanta 404
[eDate] => 2014-12-01
[suryeyor] => Amir
)

)

Я попытался с массивом данных, но пока не повезло:

Array
(
[0] => Array
(
[0] => 1
[de_ID] => 1
[1] => 1
[report_ID] => 1
[2] => 4788
[pallet_ID] => 4788
[3] => Apple
[type] => Apple
[4] => Dandrea
[consignee] => Dandrea
[5] => Stow Damage
[damage] => Stow Damage
[6] => TT Damage
[damage_desc] => TT Damage
[7] => Valdovinos
[label] => Valdovinos
[8] => Flame Seedless
[variety] => Flame Seedless
[9] => Empty and/or Missing Cartons
[category_code] => Empty and/or Missing Cartons
[10] => 2
[pieces] => 2
[11] => 2
[hold] => 2
[12] => C
[deck] => C
[13] => 2014-12-08
[dDate] => 2014-12-08
[14] => 1
[e_ID] => 1
[15] => 123
[report_number] => 123
[16] => 321
[vessel] => 321
[17] => Phoenix 602
[location] => Phoenix 602
[18] => 2014-12-01
[eDate] => 2014-12-01
[19] => Mohsin
[suryeyor] => Mohsin
)

[1] => Array
(
[0] => 2
[de_ID] => 2
[1] => 3
[report_ID] => 3
[2] => 8696
[pallet_ID] => 8696
[3] => Peach
[type] => Peach
[4] => Del Monte
[consignee] => Del Monte
[5] => Breakout Damage
[damage] => Breakout Damage
[6] => TT Damage
[damage_desc] => TT Damage
[7] => Agricom
[label] => Agricom
[8] => Mango
[variety] => Mango
[9] => Damage to contents of cartons
[category_code] => Damage to contents of cartons
[10] => 4
[pieces] => 4
[11] => 2
[hold] => 2
[12] => P
[deck] => P
[13] => 2014-12-08
[dDate] => 2014-12-08
[14] => 3
[e_ID] => 3
[15] => 526
[report_number] => 526
[16] => 748
[vessel] => 748
[17] => Atlanta 404
[location] => Atlanta 404
[18] => 2014-12-01
[eDate] => 2014-12-01
[19] => Amir
[suryeyor] => Amir
)

)

1

Решение

Ваш PHP неверен. Функция fputcsv() не знает, как обрабатывать объекты, он ожидает, что второй параметр будет массивом.

Попробуйте исправить это так:

fputcsv($output, (array)$sss);
0

Другие решения

Других решений пока нет …