我选择了上传文件路径代码,并尝试使其自动执行,这意味着我想在代码中提供文件路径,而不是用户必须上传文件
function Summit() {
var FilePath = "Summer.csv";
var fileUpload = document.getElementById("fileUpload").value;
var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.csv|.txt)$/;
if (regex.test(FilePath.value.toLowerCase())) {
if (typeof (FileReader) != "undefined") {
var reader = new FileReader();
var table = document.createElement("table");
var rows = e.target.result.split("\n");
for (var i = 0; i < rows.length; i++) {
var cells = rows[i].split(",");
if (cells.length > 1) {
var row = table.insertRow(-1);
for (var j = 0; j < cells.length; j++) {
var cell = row.insertCell(-1);
cell.innerHTML = cells[j];
}
}
var dvCSV = document.getElementById("dvCSV");
dvCSV.innerHTML = "";
dvCSV.appendChild(table);
}
reader.readAsText(fileUpload.files[0]);
} else {
alert("This browser does not support HTML5.");
}
} else {
alert("Please upload a valid CSV file.");
}
}
</script>
<button onclick="Summit()" > Done </button>
<hr />
<div id="dvCSV">
</div>