小编use*_*023的帖子

用javascript读取服务器文件

我有一个使用javascript的html页面,用户可以选择从他的PC中读取和使用自己的文本文件.但我希望在服务器上有一个示例文件,用户可以通过单击按钮打开该文件.我不知道打开服务器文件的最佳方法是什么.我用谷歌搜索了一下.(我是html和javascript的新手,所以也许我对以下内容的理解不正确!).我发现javascript是基于客户端的,打开服务器文件并不是很简单.看起来最简单的方法是使用iframe(?).所以我正在尝试(首先测试只是为了打开网页的onload)以下内容.将kgr.bss放在服务器上与我的html页面相同的目录中:

<IFRAME SRC="kgr.bss" ID="myframe" onLoad="readFile();"> </IFRAME>
Run Code Online (Sandbox Code Playgroud)

和(使用file_inhoud,其他地方定义的行)

function readFile() {
    func="readFile=";
    debug2("0");
    var x=document.getElementById("myframe");
    debug2("1");
    var doc = x.contentDocument ? x.contentDocument : (x.contentWindow.document || x.document);
    debug2("1a"+doc);
    var file_inhoud=doc.document.body;
    debug2("2:");
    lines = file_inhoud.split("\n");
    debug2("3");
    fileloaded();
    debug2("4");
}
Run Code Online (Sandbox Code Playgroud)

调试功能显示:

readFile=0//readFile=1//readFile=1a[object HTMLDocument]//
Run Code Online (Sandbox Code Playgroud)

所以停止程序的声明是:

var file_inhoud=doc.document.body;
Run Code Online (Sandbox Code Playgroud)

怎么了?读取此文件的正确(或最佳)方法是什么?

注意:我看到文件被读取并显示在框架中.

谢谢!

javascript file

6
推荐指数
2
解决办法
5万
查看次数

标签 统计

file ×1

javascript ×1