shinjuo shinjuo - 4 years ago 674
HTML Question

Reading Server Side file with Javascript

Does anyone know of a tutorial on how to read data from a server side file with JS? I cant seem to find any topics on this when I google it. I tried to use but it does not seem to work. I just want to read some data from a file to display on the page. Is this even possible?

var CSVfile = new File("test.csv");
var result ="r");
var test = result.readln();

Answer Source

To achieve this, you would have to retrieve the file from the server using a method called AJAX.

I'd look into JavaScript libraries such as Mootools and jQuery. They make AJAX very simple use.

        <script type="text/javascript" src=""></script>
        <script type="text/javascript">
            //This event is called when the DOM is fully loaded
                //Creating a new AJAX request that will request 'test.csv' from the current directory
                var csvRequest = new Request({
                        //The response text is available in the 'response' variable
                        //Set the value of the textarea with the id 'csvResponse' to the response
                        $("csvResponse").value = response;
                }).send(); //Don't forget to send our request!
        <textarea rows="5" cols="25" id="csvResponse"></textarea>

If you upload that to the directory that test.csv resides in on your webserver and load the page, you should see the contents of test.csv appear in the textarea defined.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download