在节点中设置已安装网络驱动器的路径
Set Path for Mounted Network Drive in Node
我正在尝试使用 windows-network-drive
模块和 fs
模块写入 Node 中的映射网络驱动器。
networkDrive.mount('\\server', 'Z', 'username', 'password')
.then(driveLetter => {
let filePath;
filePath = path.join(driveLetter + ":\path\to\directory", "message.txt");
fs.writeFile(filePath, "text", (err) => {
if (err) throw err;
console.log('The file has been saved!');
});
})
.catch(err => {
console.log(err)
});
如何获取写入远程位置的连接和路径?
需要传入盘符吗?如果有,我该如何找到它?
(node:4796) UnhandledPromiseRejectionWarning:
ChildProcessError: Command failed: net use Z: "\server" /P:Yes /user:username password
System error 67 has occurred.
The network name cannot be found.
net use Z: "\server" /P:Yes /user:username password
(exited with error code 2)
at callback (C:\app\location\node_modules\child-process-promise\lib\index.js:33:27)
at ChildProcess.exithandler (child_process.js:279:5)
at ChildProcess.emit (events.js:159:13)
at maybeClose (internal/child_process.js:943:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:220:5)
name: 'ChildProcessError',
code: 2,
childProcess:
{ ChildProcess: { [Function: ChildProcess] super_: [Function] },
fork: [Function],
_forkChild: [Function],
exec: [Function],
execFile: [Function],
spawn: [Function],
spawnSync: [Function: spawnSync],
execFileSync: [Function: execFileSync],
execSync: [Function: execSync] },
stdout: '',
stderr: 'System error 67 has occurred.\r\n\r\nThe network name cannot be found.\r\n\r\n' }
P.S。此代码记录 Z
networkDrive.mount('\\server\path\to\directory', 'Z', 'mdadmin', 'Password1!')
.then(function (driveLetter) {
console.log(driveLetter);
fs.writeFile('L_test.txt', 'list', (err) => {
if (err) throw err
})
});
我不确定您遇到了什么错误,所以这里有一些您在使用 windows-network-drive.
时的提示
转义特殊字符
Windows 使用 \ 分隔目录。 \ is a special character 在 JavaScript 字符串中,必须像这样转义 \\.例如C:\file.txt 在字符串中将是 C:\\file.txt.
尽可能使用 POSIX 分隔符
由于使用转义 \ 读取路径会增加难度,我建议改用 /。 windows-network-drive 应该可以很好地处理这两个问题。例如C:\file.txt 在字符串中将是 C:/file.txt.
例子
我试图使它与您的示例相匹配,但做了一些更改,以便它可以在任何 windows 机器上运行。
let networkDrive = require("windows-network-drive");
/**
* https://github.com/larrybahr/windows-network-drive
* Mount the local C: as Z:
*/
networkDrive.mount("\\localhost\c$", "Z", undefined, undefined)
.then(function (driveLetter)
{
const fs = require("fs");
const path = require("path");
let filePath;
/**
* This will create a file at "Z:\message.txt" with the contents of "text"
* NOTE: Make sure to escape '\' (e.g. "\" will translate to "\")
*/
filePath = path.join(driveLetter + ":\", "message.txt");
fs.writeFile(filePath, "text", (err) =>
{
if (err) throw err;
console.log('The file has been saved!');
});
});
要从 IIS 中托管的 REST 服务写入,您需要在服务器上正确设置权限。
- You will need to set the identity of the Application Pool of your site.
- You will need to grant write permissions to match that account or account group to the folder that you are trying to write to.
注意:如果您通过 OS 将文件夹映射到网络驱动器号,则它仅在用户帐户级别定义。
- So, if you have mapped the folder location to a drive letter (in this case 'X:'), instead of writing to
fs.writeFile('X:/test.txt', 'text', (err) => {
if (err) throw err
})
您必须写入完整路径
fs.writeFile('\\servername\path\to\director\test.txt', 'text', (err) => {
if (err) throw err
})
注意:反斜杠需要转义,所以 Windows 文件系统会显示类似 \servername\path\to\directory
.
的内容
P.S。此答案包括用户 l-bahr 和 Ctznkane525 的推荐。
我正在尝试使用 windows-network-drive
模块和 fs
模块写入 Node 中的映射网络驱动器。
networkDrive.mount('\\server', 'Z', 'username', 'password')
.then(driveLetter => {
let filePath;
filePath = path.join(driveLetter + ":\path\to\directory", "message.txt");
fs.writeFile(filePath, "text", (err) => {
if (err) throw err;
console.log('The file has been saved!');
});
})
.catch(err => {
console.log(err)
});
如何获取写入远程位置的连接和路径?
需要传入盘符吗?如果有,我该如何找到它?
(node:4796) UnhandledPromiseRejectionWarning:
ChildProcessError: Command failed: net use Z: "\server" /P:Yes /user:username password System error 67 has occurred.The network name cannot be found.
net use Z: "\server" /P:Yes /user:username password
(exited with error code 2)
at callback (C:\app\location\node_modules\child-process-promise\lib\index.js:33:27)
at ChildProcess.exithandler (child_process.js:279:5)
at ChildProcess.emit (events.js:159:13)
at maybeClose (internal/child_process.js:943:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:220:5)
name: 'ChildProcessError',
code: 2,
childProcess:{ ChildProcess: { [Function: ChildProcess] super_: [Function] },
fork: [Function],
_forkChild: [Function],
exec: [Function],
execFile: [Function],
spawn: [Function],
spawnSync: [Function: spawnSync],
execFileSync: [Function: execFileSync],
execSync: [Function: execSync] },
stdout: '',
stderr: 'System error 67 has occurred.\r\n\r\nThe network name cannot be found.\r\n\r\n' }
P.S。此代码记录 Z
networkDrive.mount('\\server\path\to\directory', 'Z', 'mdadmin', 'Password1!')
.then(function (driveLetter) {
console.log(driveLetter);
fs.writeFile('L_test.txt', 'list', (err) => {
if (err) throw err
})
});
我不确定您遇到了什么错误,所以这里有一些您在使用 windows-network-drive.
时的提示转义特殊字符
Windows 使用 \ 分隔目录。 \ is a special character 在 JavaScript 字符串中,必须像这样转义 \\.例如C:\file.txt 在字符串中将是 C:\\file.txt.
尽可能使用 POSIX 分隔符
由于使用转义 \ 读取路径会增加难度,我建议改用 /。 windows-network-drive 应该可以很好地处理这两个问题。例如C:\file.txt 在字符串中将是 C:/file.txt.
例子
我试图使它与您的示例相匹配,但做了一些更改,以便它可以在任何 windows 机器上运行。
let networkDrive = require("windows-network-drive");
/**
* https://github.com/larrybahr/windows-network-drive
* Mount the local C: as Z:
*/
networkDrive.mount("\\localhost\c$", "Z", undefined, undefined)
.then(function (driveLetter)
{
const fs = require("fs");
const path = require("path");
let filePath;
/**
* This will create a file at "Z:\message.txt" with the contents of "text"
* NOTE: Make sure to escape '\' (e.g. "\" will translate to "\")
*/
filePath = path.join(driveLetter + ":\", "message.txt");
fs.writeFile(filePath, "text", (err) =>
{
if (err) throw err;
console.log('The file has been saved!');
});
});
要从 IIS 中托管的 REST 服务写入,您需要在服务器上正确设置权限。
- You will need to set the identity of the Application Pool of your site.
- You will need to grant write permissions to match that account or account group to the folder that you are trying to write to.
注意:如果您通过 OS 将文件夹映射到网络驱动器号,则它仅在用户帐户级别定义。
- So, if you have mapped the folder location to a drive letter (in this case 'X:'), instead of writing to
fs.writeFile('X:/test.txt', 'text', (err) => {
if (err) throw err
})
您必须写入完整路径
fs.writeFile('\\servername\path\to\director\test.txt', 'text', (err) => {
if (err) throw err
})
注意:反斜杠需要转义,所以 Windows 文件系统会显示类似 \servername\path\to\directory
.
P.S。此答案包括用户 l-bahr 和 Ctznkane525 的推荐。