Javascript写入文本文件 [英] Javascript Write to Textfile

查看:104
本文介绍了Javascript写入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从表单中获取输入并将其保存到与html文件位于同一文件夹中的文本文件中.这是我到目前为止的内容:

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:

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

这确实从"item"中获取信息,并将其传递给函数writeToFile(),因为测试警报确实起作用.但是,每当我检查文件reserve.text时,都不会写入任何内容.我对javascript还是很陌生,其中大部分是我看到其他人在网上使用以获得类似效果的大量代码.有谁知道为什么它不起作用?我写的路径不正确吗?我写的脚本不正确吗?

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?

推荐答案

问题很简单:假设开发人员创建了javascript代码来遍历您的所有文件系统,并在其中填充虚拟文件,从而破坏了硬盘驱动器.过程?这就是为什么javascript不允许您执行这种操作的原因.通常,当我们要保存信息时,它是使用服务器端代码而不是客户端计算机完成的(当然,除非我们在谈论Cookie之类的信息).

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代码...最好将数据保存在客户端无法控制或编辑的地方,例如在服务器上.实例.

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.

我可能会建议一些简单的PHP代码,而不是将其存储在文本文件中,请尝试使用数据库... PHP是一种服务器端语言,可让您将内容保存到服务器计算机上的文件中,但是服务器必须能够运行PHP,大多数计算机不是使用PHP语言内置的,因此您还需要一个内置php的网络服务器.

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

这篇关于Javascript写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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