Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取节点webkit中目录的完整路径_Javascript_Node.js_Node Webkit - Fatal编程技术网

Javascript 获取节点webkit中目录的完整路径

Javascript 获取节点webkit中目录的完整路径,javascript,node.js,node-webkit,Javascript,Node.js,Node Webkit,如何在node webkit应用程序中获取目录(而不是当前工作目录)的绝对路径 示例(Mac OS)-我在我的文档中创建了一个名为a的文件夹。当我对文件系统目录条目执行getDirectory操作时,我只能获取返回A app.workspace.getDirectory(self.folderpath, {}, function(dir){ if(dir) console.log('Dir: ', dir); }); 但是我需要:~/Documents/A/|c:\Users\User

如何在node webkit应用程序中获取目录(而不是当前工作目录)的绝对路径

示例(Mac OS)-我在我的
文档中创建了一个名为
a
的文件夹。当我对文件系统目录条目执行
getDirectory
操作时,我只能获取返回
A

app.workspace.getDirectory(self.folderpath, {}, function(dir){
    if(dir) console.log('Dir: ', dir);
});
但是我需要:
~/Documents/A/
|
c:\Users\Username\Documents

在我的应用程序中,用户可以选择/创建他们想要的目录,我可以存储/读取该文件夹中的数据

绝对路径可能不是我需要的,但我希望使用默认桌面应用程序打开文件(PDF、doc…):

function getCommandLine() {
    switch (process.platform) {
        case 'darwin' : return 'open';
        case 'win32' : return 'start';
        case 'win64' : return 'start';
        default : return 'xdg-open';
    }
}

var exec = require('child_process').exec;
var filepath = '...';
//dir.fullPath will throw: The file /A/example.pdf does not exist.

var child = exec(getCommandLine() + ' ' + filepath, function (error, stdout, stderr) {
    if (error) {
        console.error(`exec error: ${error}`);
            return;
    }
});
child.on('close', function (e) {
    console.log('E: ', e);
});

我不确定我是否理解这个问题。您可以使用_dirname获取正在执行的脚本的目录。见:

从那里,您可以使用相对路径引用其他文件和文件夹

文件夹结构示例:

/home/filippo/works/test/
├── subfolder
│   └── index.js
└── test.js
子文件夹/index.js:

module.exports = {'folder': __dirname};
test.js:

var m = require("./subfolder/");

console.log("test.js running in: ", __dirname);
console.log("m module running in: ", m.folder);
运行测试:

$ node test.js 
test.js running in:  /home/filippo/works/test
m module running in:  /home/filippo/works/test/subfolder

这就是您要查找的吗?

出于安全原因,您无法从网页访问系统目录文件的绝对路径。即使您尝试从javascript脚本(例如)访问特定的文件目录,浏览器也会立即阻止您


一个可能适用于您的解决方案是检索用户希望使用简单的
打开的文件,以便用户可以选择一个文件,而无需向您提供其绝对路径;之后,您可以将文件保存到服务器主机,甚至项目的本地目录,然后使用exec命令使用远程路径打开它。

事实上,我最近发现了(我不敢相信我一开始就漏掉了它),使用chrome文件系统,您可以获得“更可读”的路径信息以供显示

根据您尝试的操作,您可能无法访问绝对位置,但至少您可以看到它是什么

fileSystem.getDirectory(path, {}, function(dir){
    if(dir) console.log('This should return full path: ', dir.getDisplayPath());
    var fullpath = dir.getDisplayPath();

    //now with this, I can open any file I want via child process :)

    var child = exec(getCommandLine() + ' ' + fullpath, function (error, stdout, stderr) 
    //.......
});

有关chrome文件系统的更多信息。

是的,您还没有理解:)脚本的目录和我要访问的文件夹是分开的。两者都可以在任何地方。脚本显然是从应用程序包运行的,理想情况下,应用程序包应该位于Applications文件夹中,而另一个目录是自由选择的。用户可以在其计算机上的任何位置选择任何文件夹。我真正想做的是用用户的默认应用程序打开文档,我找到了一种使用exec的方法,但是shell脚本是从根目录运行的……好的,对不起。我猜你不能把用户选择的文件夹存储在某个地方。不是完整的URL不-它是沙盒。我已经在我的
~/Documents/Myfolder
中创建了我的文件夹。。我只能返回
/Myfolder