用Javascript读取服务器端文件 [英] Reading Server Side file with Javascript

查看:112
本文介绍了用Javascript读取服务器端文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何从JS服务器端文件读取数据的教程?我无法在Google上找到任何有关此主题的内容。我试图使用,但似乎没有工作。我只想从文件中读取一些数据以显示在页面上。这甚至有可能吗?

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 = CVSfile.open("r");
var test = result.readln();


推荐答案

为了达到这个目的,您必须检索文件从服务器使用名为AJAX的方法。

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

我会研究JavaScript库(如Mootools和jQuery)。它们让AJAX非常简单的使用。

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

<html>
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/mootools/1.6.0/mootools-core.min.js"></script>
        <script type="text/javascript">
            //This event is called when the DOM is fully loaded
            window.addEvent("domready",function(){
                //Creating a new AJAX request that will request 'test.csv' from the current directory
                var csvRequest = new Request({
                    url:"test.csv",
                    onSuccess:function(response){
                        //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!
            });
        </script>
    </head>
    <body>
        <textarea rows="5" cols="25" id="csvResponse"></textarea>
    </body>
</html>

如果您将其上传到test.csv驻留在您的Web服务器上并加载该页面的目录,您应该看到test.csv的内容出现在定义的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.

这篇关于用Javascript读取服务器端文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆