javascript Node-Webkit:如何在本地文件系统中创建文件?

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

Node-Webkit: How to create a file in the local file system?

javascriptnode-webkit

提问by cohoman

With using Node-Webkit, I'm trying to create and write to a text file in a user defined local folder from within a web worker js file. I tried using requestFileSystemSync(), .root.getFile(), .createWriter(), etc. but I'm not sure where this file is saved (can it be written to an defined local folder, or is it "sandboxed" to a special folder location?).

通过使用 Node-Webkit,我尝试从 Web Worker js 文件中创建并写入用户定义的本地文件夹中的文本文件。我尝试使用requestFileSystemSync().root.getFile().createWriter()等,但我不确定此文件的保存位置(可以将其写入定义的本地文件夹,还是“沙盒化”到特殊文件夹位置?)。

Can anyone offer advice on the best way for me to create text files from a web worker to an arbitrary local folder location? That is, in the local file system outside of a sandboxed file system?

任何人都可以就我从网络工作者到任意本地文件夹位置创建文本文件的最佳方式提供建议吗?也就是说,在本地文件系统之外的沙盒文件系统?

Thanks.

谢谢。

回答by Nathan Breit

It sounds like you might be trying to use the web filesystem API. That might be a reasonable solution, it would allow you to run your app as a regular webpage, however the web filesystem API is sandboxed.

听起来您可能正在尝试使用 Web 文件系统 API。这可能是一个合理的解决方案,它允许您将应用程序作为常规网页运行,但是 Web 文件系统 API 是沙盒的。

node-webkit offers "Complete support for Node.js APIs," which I think means you can use the node.js file system APIto write anywhere on the file system. Pretty cool. So something like this should work:

node-webkit 提供“对 Node.js API 的完全支持”,我认为这意味着您可以使用node.js 文件系统 API在文件系统上的任何位置进行编写。很酷。所以这样的事情应该有效:

var fs = require('fs');
fs.writeFile("path/to/anywhere/test.txt", "Hi mom!", function(err) {
    if(err) {
        alert("error");
    }
});

Also, node-webkit web-workers do not support require I hear. So you'll need to do your filesystem stuff in your main thread.

另外,我听说 node-webkit 网络工作者不支持 require。所以你需要在你的主线程中做你的文件系统的事情。