我的程序采用2D数组并将其转换为可以像这样下载的csv:
var csvRows = [];
for (var i = 0; i < twoDiArray.length; ++i) {
for (var j = 0; j < twoDiArray[i].length; ++j) {
twoDiArray[i][j] = '\"' + twoDiArray[i][j] + '\"'; // Handle elements that contain commas
twoDiArray[i][j] = twoDiArray[i][j].replace(/ /g, '%20');
}
csvRows.push(twoDiArray[i].join(','));
}
var csvString = csvRows.join('\r\n');
var a = document.createElement('a');
a.href = 'data:attachment/csv,' + csvString;
a.target = '_blank';
a.download = 'MD_Database_with_Coords.csv';
document.body.appendChild(a);
a.click();
然后我如何保存此文件而无需再次上载到服务器文件夹中,以便之后可以立即使用另一个窗口(使用php访问cvs文件)加载该文件。我在新窗口中的php代码是:
<?php
//opens file
$file = fopen("MD_Database_with_Coords.csv","r");
$array_of_array = array();
$counter = 0;
//reads entries
while(!feof($file)) {
$array_entry = fgetcsv($file);
//ignore first line
if ($array_entry[0] == "ID") {
continue;
}
//fill array with entries
if ($array_entry != NULL) {
$array_of_array[$counter] = $array_entry;
}
$counter++;
}
fclose($file);
?>