简体   繁体   English

如何使用 javascript/jquery 将表单信息写入本地 txt 存档?

[英]How to write the form information to a local txt archive using javascript/jquery?

I need to write a js function to write the form information into a .txt我需要写一个js函数将表单信息写入.txt

i will not use database in this project, i will use .txt files to control everything, products, person, employee , etc. All they will be at a .txt called control.txt with the information like this:我不会在这个项目中使用数据库,我将使用 .txt 文件来控制一切,产品,人员,员工等。所有这些都将在一个名为 control.txt 的 .txt 中,其中包含如下信息:

[person]name|email|date [人]姓名|电子邮件|日期

[employee]name|department [员工]姓名|部门

So i need a function that writes the form information into my .txt archive.所以我需要一个将表单信息写入我的 .txt 存档的函数。

You can't.你不能。

Think this through for a minute, you want javascript code on a users computer to be able to directly edit a file on your webserver.想一想,您希望用户计算机上的 javascript 代码能够直接编辑您的网络服务器上的文件。

The security risks of allowing writing to arbitrary files on a server would be insane.允许写入服务器上的任意文件的安全风险将是疯狂的。 Not to mention handling conflicts with multiple clients.更不用说处理与多个客户端的冲突了。

There is a reason why databases are used.使用数据库是有原因的。 If you want to modify data on a server you need some kind of server side layer.如果要修改服务器上的数据,则需要某种服务器端层。

While I don't really recommend doing this through text file.虽然我真的不建议通过文本文件执行此操作。 You may find filesaver.js useful in manipulating text file https://github.com/eligrey/FileSaver.js .您可能会发现 filesaver.js 在操作文本文件https://github.com/eligrey/FileSaver.js 时很有用。

However as @lod pointed out, you can't really edit the text files of user directly.但是正如@lod 指出的那样,您不能真正直接编辑用户的文本文件。 That's why there are fakepath whenever we put a file in a file upload component这就是为什么每当我们将文件放入文件上传组件时都会有 fakepath

Your best bet is to save the text file in server and read it from there.最好的办法是将文本文件保存在服务器中并从那里读取。 Once you are done editing/creating the text.完成编辑/创建文本后。 upload it back in the server.将其上传回服务器。

But this sounds more tedious rather than creating a simple database service imho但这听起来比创建一个简单的数据库服务更乏味恕我直言

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM