【发布时间】:2012-02-08 04:30:01
【问题描述】:
我正在尝试制作一个可以读取服务器端 CSV 文件并以简洁、可浏览的格式显示的网络应用程序。问题是我想用裸 JS/CSS3/HTML5 来做。 有没有办法用裸 JS/CSS3/HTML5 读写服务器端文件?我显然希望它独立于操作系统/浏览器。
我尝试过的
我已经尝试实现了一些我在网上找到的代码(一些网站引用了它)。以下是我在测试时尝试过的:(我只想让测试在网页中显示网页本身的内容)
<html>
<head>
<script type="text/javascript" src="readIt.JS"></script>
</head>
<body>
<button onclick="return readIt();">Show the code of the page</button>
<div id="readItOutput"></div>
</body>
</html>
function readIt()
{
file = fopen(getScriptPath("scriptTest.htm"), 0);
file_length = flength(file);
content = fread(file, file_length);
document.getElementById("readIt").innerText = content;
}
但是,每当我在 Opera 和 Chrome 下运行它时,它都会抛出以下内容:
歌剧:
Uncaught exception: ReferenceError: Undefined variable: fopen
Error thrown at line 3, column 1 in readIt() in http://s.supuhstar.operaunite.com/s/content/JS/readIt.JS:
file = fopen(getScriptPath("scriptTest.htm"), 0);
called from line 1, column 0 in <anonymous function>(event) in http://s.supuhstar.operaunite.com/s/content/JS/scripttest.htm:
return readIt();
铬:
Uncaught ReferenceError: getScriptPath is not defined
readItreadIt.JS:3
(anonymous function)scripttest.htm:6
onclick
【问题讨论】:
-
您是否考虑过使用 XHR ("Ajax") 拉取所需文件,然后解析逗号和行?您不能使用此方法直接编写服务器文件(您必须通过 XHR 请求服务器端脚本为您执行此操作),但您可以通过这种方式“读取”文件并解析其内容。
-
马特完全正确。您可以使用 AJAX 获取文件内容,然后根据需要进行格式化。
-
@Matt XHR 的哪一部分是“naked JS/CSS3/HTML5”?
-
@Supuhstar 几乎全部:developer.mozilla.org/en/XMLHttpRequest
标签: javascript file file-io fopen fread