仅使用 JavaScript 将数据保存到本地文件

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

Saving data to a local file using only JavaScript

javascriptgoogle-chrome

提问by sidonaldson

The set-up in question:

有问题的设置:

I have a stand alone, offline, kiosk mode instance of Chrome running on a Windows machine. I have full access to the system and any admin rights. I can start Chrome with any flags set or unset.

我有一个在 Windows 机器上运行的独立、离线、自助服务终端模式的 Chrome 实例。我拥有对系统的完全访问权限和任何管理员权限。我可以在设置或取消设置任何标志的情况下启动 Chrome。

The task:

任务:

I have been asked to create a log file which tracks user activity within the offlineapp I am coding. It's a simple form of analytics which will append each event to the end of the file separated with a comma. The file will then be sent to a server once a day via a scheduled task. (None of this is my idea so please don't troll me)

我被要求创建一个日志文件来跟踪我正在编写的离线应用程序中的用户活动。这是一种简单的分析形式,它将每个事件附加到以逗号分隔的文件末尾。然后,该文件将通过计划任务每​​天发送一次到服务器。(这一切都不是我的想法,所以请不要欺骗我)

Ruled out: Any server side code- I have lobbied for Node, PHP etc but as this will be distributed to many different installations so we cannot guarantee this will be installed. Flash/ActiveX/Java - ideally would like to avoid this (even though these will be installed by default)

排除: 任何服务器端代码- 我已经为 Node、PHP 等进行了游说,但因为这将分发到许多不同的安装中,所以我们不能保证会安装它。Flash/ActiveX/Java - 理想情况下希望避免这种情况(即使默认情况下会安装这些)

Possible solutions: File API - I have looked at this but AFAIKif opens dialogue boxes to save the data to each file and this needs to happen in the background. Security - I have read in other SO Questions that this can be achieved if the security settings are reduced but no-one goes on to explain which ones. Is there a simple flag which allows it? How to enable local file system read write access from Google chrome?- similar question!

可能的解决方案:文件 API - 我已经看过这个,但AFAIK如果打开对话框将数据保存到每个文件,这需要在后台发生。安全性 - 我在其他 SO Questions 中读到,如果减少安全设置但没有人继续解释哪些是可以实现的。是否有一个简单的标志允许它? 如何从 Google chrome 启用本地文件系统读写访问?- 类似的问题!

Ideal result: (something akin to PHP)

理想的结果:(类似于 PHP)

$file = 'log.txt';
$current = file_get_contents($file);
$current .= ",clicked:link";
file_put_contents($file, $current);

Possible ideal side result: proving this isn't possible and forcing PHP/Node/Java to be used ;)

可能的理想副结果:证明这是不可能的并强制使用 PHP/Node/Java ;)

In reply to those suggesting local storage : I'm not storing unique key/value pairs and that is very much like setting a cookie. Similarily there are file size limits. To those suggesting web SQL such as SQLite in chrome - there are file size limits if it's not a chrome extension. The only way I see that working is if I were to find the location of the file in the windows directory (C:\Users\%USERNAME%\AppData\Local\Google\Chrome\User Data\Default\databases) and upload that from the schedules task. Perfectly feasible but it is not a desirable answer.

回复那些建议本地存储的人:我没有存储唯一的键/值对,这很像设置 cookie。同样有文件大小限制。对于那些建议使用 web SQL(例如 chrome 中的 SQLite)的人 - 如果它不是 chrome 扩展名,则存在文件大小限制。我看到工作的唯一方法是,如果我要在 windows 目录 (C:\Users\%USERNAME%\AppData\Local\Google\Chrome\User Data\Default\databases) 中找到文件的位置并上传从计划任务。完全可行,但不是理想的答案。

采纳答案by Kinlan

You can use the Chrome Apps File API, you will need to grant access to the file via a user action once, but after that you can get access the file again by using restoreEntry

您可以使用Chrome Apps File API,您需要通过一次用户操作授予对文件的访问权限,但之后您可以通过使用再次访问该文件restoreEntry

回答by Ryan

You could use HTML5?

你可以使用 HTML5 吗?

http://diveintohtml5.info/storage.html

http://diveintohtml5.info/storage.html

var foo = localStorage.getItem("bar");
// ...
localStorage.setItem("bar", foo);

回答by Melvin

You can use localStorageto save offline data. It's impossible to access other files using Javascript since it's a violation of the sandbox.

您可以使用localStorage来保存离线数据。使用 Javascript 访问其他文件是不可能的,因为它违反了沙箱。

From http://en.wikipedia.org/wiki/JavaScript#Security:

来自http://en.wikipedia.org/wiki/JavaScript#Security

JavaScript and the DOM provide the potential for malicious authors to deliver scripts to run on a client computer via the web. Browser authors contain this risk using two restrictions. First, scripts run in a sandbox in which they can only perform web-related actions, not general-purpose programming tasks like creating files.

JavaScript 和 DOM 为恶意作者提供了通过 Web 提供在客户端计算机上运行的脚本的可能性。浏览器作者使用两个限制来控制这种风险。首先,脚本在沙箱中运行,在沙箱中它们只能执行与 Web 相关的操作,而不能执行诸如创建文件之类的通用编程任务。

回答by Buchow_PHP

You may want to look into Local Storagewhich is part of the HTML5 spec. This will only be supported in modern browsers though.

您可能需要查看本地存储,它是 HTML5 规范的一部分。不过,这只会在现代浏览器中受支持。

If you need to use older browsers then may still be able to achieve what you're after using dojox.storage

如果您需要使用较旧的浏览器,那么在使用dojox.storage后仍然可以实现您的目标

回答by Arunprasad Rajkumar

Use HTML5 features like Web Storageor Web SQLdatabase to store your logs.

使用Web 存储Web SQL数据库等 HTML5 功能来存储您的日志。

Whenever needed read logs from the client side storage and send it back to the server & delete the client storage.

需要时从客户端存储读取日志并将其发送回服务器并删除客户端存储。

Refer http://www.html5rocks.com/en/features/storage.

请参阅http://www.html5rocks.com/en/features/storage