我正在尝试使用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:命令失败:net use Z:" \ server" / P:是/用户:用户名密码 系统错误67已发生。找不到网络名称。
net use Z: "\server" /P:Yes /user:username password
(退出时显示错误代码2)
在回调(C:\ app \ location \ node_modules \ child-process-promise \ lib \ index.js:33:27)
在ChildProcess.exithandler(child_process.js:279:5)
在ChildProcess.emit(events.js:159:13)
在maybeClose(internal / child_process.js:943:16)
在Process.ChildProcess._handle.onexit(internal / child_process.js:220:5)
name:' ChildProcessError',
代码:2,
childProcess:{ChildProcess:{[Function:ChildProcess] super_:[Function]},
fork:[功能],
_forkChild:[功能],
执行:[功能],
execFile:[功能],
产卵:[功能],
spawnSync:[功能:spawnSync],
execFileSync:[功能:execFileSync],
execSync:[功能:execSync]},
标准输出:'',
stderr:'系统错误67已发生。\ r \ n \ r \ n无法找到网络名称。\ 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
})
});
答案 0 :(得分:1)
要从IIS中托管的REST服务进行写入,您需要在服务器上正确设置权限。
- 您需要设置站点的应用程序池的标识。
醇>
- 您需要授予写入权限,以便将该帐户或帐户组与您尝试写入的文件夹相匹配。
醇>
注意:如果您通过操作系统将文件夹映射到网络驱动器号,则仅在用户帐户级别定义。
- 因此,如果您已将文件夹位置映射到驱动器号(在本例中为' X:'),而不是写入
醇>
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的推荐。
答案 1 :(得分:0)
我不确定你有什么错误,所以这里有一些关于你何时使用windows-network-drive. 的提示
逃避特殊字符
Windows使用\来分隔目录。 JavaScript字符串中的\ is a special character,必须像此\\一样进行转义。例如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!');
});
});