如何读取服务器上使用JavaScript的文本文件? [英] How to read a text file from server using JavaScript?

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

问题描述

在服务器上,还有一个文本文件。在客户端上使用JavaScript,我希望能够读取该文件,并对其进行处理。在服务器上的文件的格式,不能更改。

On the server, there is a text file. Using JavaScript on the client, I want to be able to read this file and process it. The format of the file on the server cannot be changed.

我怎样才能得到文件到JavaScript变量的内容,所以我可以做这样的处理?该文件的大小可高达3.5 MB,但是它可以很容易地在,比方说,100行组块进行处理(1行是50-100个字符)。

How can I get the contents of the file into JavaScript variables, so I can do this processing? The size of the file can be up to 3.5 MB, but it could easily be processed in chunks of, say, 100 lines (1 line is 50-100 chars).

没有的文件的内容应该是可见给用户;他将看到该文件中的数据的处理的结果。

None of the contents of the file should be visible to the user; he will see the results of the processing of the data in the file.

推荐答案

您可以使用隐藏帧,加载该文件在那里并解析其内容。

You can use hidden frame, load the file in there and parse its contents.

HTML:

<iframe id="frmFile" src="test.txt" onload="LoadFile();" style="display: none;"></iframe>

JavaScript的:

JavaScript:

<script type="text/javascript">
function LoadFile() {
    var oFrame = document.getElementById("frmFile");
    var strRawContents = oFrame.contentWindow.document.body.childNodes[0].innerHTML;
    while (strRawContents.indexOf("\r") >= 0)
        strRawContents = strRawContents.replace("\r", "");
    var arrLines = strRawContents.split("\n");
    alert("File " + oFrame.src + " has " + arrLines.length + " lines");
    for (var i = 0; i < arrLines.length; i++) {
        var curLine = arrLines[i];
        alert("Line #" + (i + 1) + " is: '" + curLine + "'");
    }
}
</script>

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

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