需要从Javascript代码中的html表在cpanel目录中创建csv文件

时间:2018-11-14 06:11:42

标签: javascript

我可以在以下JavaScript代码的帮助下从html表下载csv文件;

function downloadCSV(csv, filename) {
var csvFile;
var downloadLink;

// CSV file
csvFile = new Blob([csv], {type: "text/csv"});

// Download link
downloadLink = document.createElement("a");

// File name
downloadLink.download = filename;

// Create a link to the file
downloadLink.href = window.URL.createObjectURL(csvFile);

// Hide download link
downloadLink.style.display = "none";

// Add the link to DOM
document.body.appendChild(downloadLink);

// Click download link
downloadLink.click();
}

function exportTableToCSV(filename) {
var csv = [];
var rows = document.querySelectorAll(".csv");

for (var i = 0; i < rows.length; i++) {
var row = []; 

cols = rows[i].querySelectorAll(".csvs");
            // cols =   cols.replace(",","");

for (var j = 0; j < cols.length; j++) 
row.push(cols[j].innerText.replace(",",""));

csv.push(row.join(","));        
}

// Download CSV file //// now need to create file in cpanel dir.
downloadCSV(csv.join("\n"), filename);
}

现在需要在cpanel目录中创建csv文件,但无需下载。请更改相同的代码,以便在更改csv文件的过程中使用它。

1 个答案:

答案 0 :(得分:0)

JavaScript是一种客户端语言,因此它无法执行与服务器有关的任何功能,因此需要PHP脚本来实现。