用 JavaScript 读/写文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9187860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:52:21  来源:igfitidea点击:

Reading/Writing files in JavaScript

javascriptfilefile-iofopenfread

提问by Supuhstar

I'm trying to make a web app that will read in a server-side CSV file and display it in a neat, browsable format. The catch is that I want to do it in naked JS/CSS3/HTML5. Is there any way to read and write server-side files with naked JS/CSS3/HTML5?I obviously want this to be OS/browser independent.

我正在尝试制作一个 Web 应用程序,它将读取服务器端 CSV 文件并以整洁、可浏览的格式显示它。问题是我想在裸 JS/CSS3/HTML5 中做到这一点。有没有办法用裸JS/CSS3/HTML5读写服务器端文件?我显然希望这是独立于操作系统/浏览器的。

What I've tried

我试过的



I havetried implementing some code I found online (a few sites reference it). Below is what I tried while testing: (I just want the test to show the contents of the webpage, itself, in the webpage)

已经尝试过推行一些代码,我在网上找到的(一些网站引用它)。以下是我在测试时尝试的内容:(我只想让测试在网页中显示网页本身的内容)

scriptTest.htm:

scriptTest.htm

<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>

readIt.JS:

readIt.JS

function readIt()
{
    file = fopen(getScriptPath("scriptTest.htm"), 0);
    file_length = flength(file);
    content = fread(file, file_length);
    document.getElementById("readIt").innerText = content;
}

However, whenever I run it, under Opera and Chrome, it throws the following:

但是,每当我在 Opera 和 Chrome 下运行它时,它都会抛出以下内容:

Opera:

歌剧:

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();

Chrome:

铬合金:

Uncaught ReferenceError: getScriptPath is not defined
readItreadIt.JS:3
(anonymous function)scripttest.htm:6
onclick

回答by Alexander

If you want to edit some files from server you need to use XHR object to download file to client side and again use XHR object to send modifed data back to server, also you need some sort of API on you server to send/recieve data.

如果您想从服务器编辑一些文件,您需要使用 XHR 对象将文件下载到客户端,然后再次使用 XHR 对象将修改后的数据发送回服务器,您还需要在服务器上使用某种 API 来发送/接收数据。

回答by Francis Lewis

File writing cannot be done with JS/CSS3/HTML5 alone for security reasons, otherwise people would be able to modify the js in FireBug and write a file. You would need to create an API of some sort using either server-side JS or a language such as PHP to handle the permissions, file names, file locations, etc…

出于安全考虑,不能单独使用JS/CSS3/HTML5进行文件写入,否则人们就可以在FireBug中修改js并写入文件。您需要使用服务器端 JS 或 PHP 等语言创建某种 API 来处理权限、文件名、文件位置等……

As for reading, your file would have to be publicly accessible, otherwise you'll need it served by a server-side language such as PHP.

至于阅读,您的文件必须是可公开访问的,否则您将需要由服务器端语言(如 PHP)提供服务。