Javascript 写入文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39123684/
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
Javascript Write to Textfile
提问by khm
I'm trying to take input from a form and save it into a text file that is in the same folder as the html file. This is what I have so far:
我正在尝试从表单中获取输入并将其保存到与 html 文件位于同一文件夹中的文本文件中。这是我到目前为止:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Reservation</title>
<meta name="description" content="The HTML5 Herald">
<meta name="author" content="SitePoint">
<link rel="stylesheet" href="css/styles.css?v=1.0">
<script>
function writeToFile(item, name, time)
{
alert("Hello " + item);
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fh = fso.OpenTextFile("E:/labChart/etc/reserve.text", 8);
fh.WriteLine(item);
fh.Close();
}
function readFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fh = fso.OpenTextFile("reserve.text", 1, false, 0);
var lines = "";
while (!fh.AtEndOfStream) {
lines += fh.ReadLine() + "\r";
}
fh.Close();
return lines;
}
</script>
</head>
<body>
Reservation
<br>
<form>
Item:
<br>
<input type="text" name="item" id="item">
<br>
Name:
<br>
<input type="text" name="name" id="name">
<br>
Time:
<br>
<input type="date" name="time" id="time">
<br>
<br>
<input type="submit" value="Submit" onclick="writeToFile(document.getElementById('item').value, document.getElementById('name').value, document.getElementById('time').value)">
</form>
<script src="js/scripts.js"></script>
</body>
</html>
This does take the info from "item" and pass it to the function writeToFile() because the test alert does work. But whenever I check the file reserve.text nothing is written there. I'm very new to javascript and most of this is an amalgamation of code I saw other people using online for similar effects. Does anyone know why it is not working? Am I writing the path incorrectly? Am I not writing the script correctly?
这确实从“item”获取信息并将其传递给函数 writeToFile() 因为测试警报确实有效。但是每当我检查文件 reserve.text 时,那里都没有写任何内容。我对 javascript 非常陌生,其中大部分是我看到其他人在线使用以获得类似效果的代码的合并。有谁知道为什么它不起作用?我写的路径不正确吗?我没有正确编写脚本吗?
采纳答案by Max Alexander Hanna
The problem with this is simple: Lets say a developper created javascript code to go through all your filesystem and populate it with dummy files, ruining your hard drive in the process? That is why javascript won't allow you to do this kind of operation. When we want to save information, usually, its done using server-side code and not the client's computer (unless of course we are talking about things like cookies).
这个问题很简单:假设开发人员创建了 javascript 代码来遍历您的所有文件系统并用虚拟文件填充它,在此过程中破坏了您的硬盘驱动器?这就是为什么 javascript 不允许你做这种操作的原因。当我们想要保存信息时,通常是使用服务器端代码而不是客户端的计算机来完成的(当然,除非我们谈论的是 cookie 之类的东西)。
The point of my answer is to let you rethink who does the saving and to where. It should be up to the server to save and retain any information for a user, and so you would not write this kind of javascript code... It is best to save data somewhere your client cannot control or edit, like on the server for instance.
我的回答的重点是让您重新思考谁在储蓄以及向何处储蓄。应该由服务器来保存和保留用户的任何信息,所以你不会编写这种 javascript 代码......最好将数据保存在客户端无法控制或编辑的地方,比如在服务器上实例。
I could suggest some easy PHP code, and instead of storing inside a text file, try a database... PHP is a server-side language which will let you save things to files on your server computer, however your server must be able to run PHP, most computers don't come built in with the PHP language and so you will also need a webserver with php built-in..
我可以推荐一些简单的 PHP 代码,而不是存储在文本文件中,而是尝试数据库...... PHP 是一种服务器端语言,它可以让您将内容保存到服务器计算机上的文件中,但是您的服务器必须能够运行 PHP,大多数计算机都没有内置 PHP 语言,因此您还需要一个内置 php 的网络服务器。
回答by Chris G
In my opinion your entire approach is bad; you need to learn PHP and mySQL to store and load persistent data. - Edit:accessing the file system from JavaScript is a huge security risk and therefore not allowed in general. Unless your goal is specifically to write files from JS, there are better alternatives.
在我看来,您的整个方法都很糟糕;您需要学习 PHP 和 mySQL 来存储和加载持久数据。-编辑:从 JavaScript 访问文件系统存在巨大的安全风险,因此一般不允许。除非您的目标是专门从 JS 编写文件,否则还有更好的选择。
Anyway, this code will only work on Internet Explorer, and only with the security settings way down. You shouldn't pursue this though.
无论如何,这段代码只能在 Internet Explorer 上运行,并且只能在安全设置下降的情况下使用。不过你不应该追求这个。
If your end goal is to write a web app that stores and displays reservations, get XAMPP and find a beginner PHP + database tutorial.
如果您的最终目标是编写一个存储和显示预订的 Web 应用程序,请获取 XAMPP 并找到 PHP + 数据库初学者教程。