仅使用 javascript 在服务器上读取/写入 txt 文件而不涉及任何服务器端语言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29898158/
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-28 11:15:47  来源:igfitidea点击:

Read / Write txt file on server using only javascript without involving any server side language

javascript

提问by meexplorer

I'm working on a simple HTML/Javascript application. This application shows a random text message to the user in a text area. I have 15-20 such text messages of at most 500 characters. I don't want to save these messages in database. I have two scenarios -

我正在开发一个简单的 HTML/Javascript 应用程序。此应用程序在文本区域中向用户显示随机文本消息。我有 15-20 条最多 500 个字符的此类短信。我不想将这些消息保存在数据库中。我有两种情况——

  1. I have multiple .txt files for each message and then by using javascript only to read a random txt file and display file's content in textarea.
  2. User enters a message in a separate text area and by pressing save button, a new txt file for that message should be created.
  1. 我为每条消息有多个 .txt 文件,然后仅使用 javascript 读取随机 txt 文件并在 textarea 中显示文件的内容。
  2. 用户在单独的文本区域中输入一条消息,然后按保存按钮,应该为该消息创建一个新的 txt 文件。

I've been searching how to read / write files in javascript, I found these two posts - Is it possible to write data to file using only JavaScript?and How to read and write into file using JavaScript.
But these posts are about reading file on client side or reading file from file input type.

我一直在搜索如何在 javascript 中读取/写入文件,我发现了这两篇文章 - 是否可以仅使用 JavaScript 将数据写入文件?以及如何使用 JavaScript 读取和写入文件
但这些帖子是关于在客户端读取文件或从文件输入类型读取文件。

https://stackoverflow.com/a/21012689/1930283

https://stackoverflow.com/a/21012689/1930283

This suggestion is about using some server side language.
Is there any way to read and write txt files using Javascript only.

这个建议是关于使用一些服务器端语言。
有什么方法可以只使用 Javascript 读取和写入 txt 文件。

回答by Quentin

Only if JavaScript is your server side language (e.g. with Node.JS).

仅当 JavaScript 是您的服务器端语言时(例如使用 Node.JS)。

Servers don't let you write files to them by default. That would be a horrible security problem.

默认情况下,服务器不允许您将文件写入它们。那将是一个可怕的安全问题。

回答by Micaiah Wallace

You wouldn't want any write access to files on your server from the client side code, you would easily get hacked pretty quickly. But in terms of reading, you could just host the txt files in the public html directory and perform an async get request from javascript using jquery like so:

您不希望从客户端代码对服务器上的文件进行任何写访问,您很容易很快被黑客入侵。但在阅读方面,您可以将 txt 文件托管在公共 html 目录中,并使用 jquery 执行来自 javascript 的异步获取请求,如下所示:

$.get("http://../file.txt", function(data) {
console.log("Here's the file data: "+data);
});

EDIT:The reason js can't manipulate files on the server by itself is because a browser can only make requests to a server using a URL and a PORT. What happens when the server gets that request is all up to the server side code or program that's hosting the files like apache.

编辑:js 无法单独操作服务器上的文件的原因是浏览器只能使用 URL 和 PORT 向服务器发出请求。当服务器收到该请求时会发生什么完全取决于托管诸如 apache 之类的文件的服务器端代码或程序。

回答by user2707695

I make a weblink to my server and use FileSaver.js to manually save whatever data to any file on my server via that weblink. This is simple and save, but needs manual intervention.

我创建了一个到我的服务器的网络链接,并使用 FileSaver.js 通过该网络链接手动将任何数据保存到我服务器上的任何文件中。这个简单省事,但是需要人工干预。

回答by Siguza

If you have a server-side service like FTP or SSH running, then yes, it is possible, but it is then also possible for anyoneto read your username and password from you JavaScript file and log in to your servervia the service you use.
So this should never be done.

如果您正在运行 FTP 或 SSH 等服务器端服务,那么是的,这是可能的,但是任何人也可以从您的 JavaScript 文件中读取您的用户名和密码并通过您使用的服务登录到您的服务器.
所以这永远不应该做

If there is no server-side service running and you have no own server-side script, then no, it is not possible. Just like it isn't possible for me to just create files on your computer.

如果没有服务器端服务在运行并且您没有自己的服务器端脚本,那么不,这是不可能的。就像我不可能在您的计算机上创建文件一样。