Auto Update csv file in HTML Table with Ajax

0

I have a JS code here and it runs with Ajax. In the subject created I would like to load the csv already selected in the site, and every time I reload the page it loads the csv in the html table. No need to select and thus generating Auto Update.

The code line looks like this:

<html>
<head>
    <title>CSV to Table</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script><scripttype="text/javascript">
        $(function () {
            $("#upload").bind("click", function () {
                var regex = /^([a-zA-Z0-9\s_\.\-:])+(.csv|.txt)$/;
                if (regex.test($("#fileUpload").val().toLowerCase())) {
                    if (typeof (FileReader) != "undefined") {
                        var reader = new FileReader();
                        reader.onload = function (e) {
                            var table = $("<table />");
                            var rows = e.target.result.split("\n");
                            for (var i = 0; i < rows.length; i++) {
                                var row = $("<tr />");
                                var cells = rows[i].split(",");
                                if (cells.length > 1) {
                                    for (var j = 0; j < cells.length; j++) {
                                        var cell = $("<td />");
                                        cell.html(cells[j]);
                                        row.append(cell);
                                    }
                                    table.append(row);
                                }
                            }
                            $("#dvCSV").html('');
                            $("#dvCSV").append(table);
                        }
                        reader.readAsText($("#fileUpload")[0].files[0]);
                    } else {
                        alert("This browser does not support HTML5.");
                    }
                } else {
                    alert("Please upload a valid CSV file.");
                }
            });
        });
    </script>
</head>

<body>
    <input type="file" id="fileUpload" />
    <input type="button" id="upload" value="Upload" />
    <hr />
    <div id="dvCSV">
    </div>
</body>

</html>

Can anyone help me? (I am not an expert on the front, but if you can explain and exemplify thank you.)

    
asked by anonymous 31.10.2018 / 17:51

0 answers