koa+SMB2实现跨电脑文件读写

1.安装smb2插件

npm install smb2 -s

2.在需要写入的电脑中设置共享文件夹

3.获取需要写入电脑ip地址

        >> cmd

        >> ipconfig

4. 在koa中编写

   const smbConfig = {
     share: "\\\\remote-server-ip\\share-name",
     domain: "your-domain", // 可选,根据你的网络配置
     username: "your-username",
     password: "your-password",
   };

注:①remote-server-ip:写入电脑的ip

       ②share-name:共享文件夹名

       ③your-domain:电脑域(没有可不填) ==>我的电脑/此电脑->右键->属性  可查看

       ④your-username 电脑用户名

       ⑤your-password 电脑密码

5. 创建对象SMB2对象

const smbClient = new SMB2(smbConfig);

 6. 写入

smbClient.writeFile('file.txt', 'Hello Node', function(err) {
  if (err) throw err;
  console.log("It's saved!");
});

注:第一个参数直接写文件名即可,不用带上共享文件夹地址,第二个参数可直接写需要写入的内容

6.1 写入文件

smbClient.writeFile(
    remoteFilePath,
    fs.readFileSync(localFilePath),
    (err) => {
        if (err) {
            throw err;
        }
        console.log("File uploaded to remote server:", remoteFilePath);
        ctx.body = "File uploaded!";
    }
);

注:①remoteFilePath:写入的地址,没有文件夹则直接写文件名

       ②fs.readFileSync(localFilePath):smb2的riteFile第二个参数只能传入字符串或者buffer值,所以使用readFileSync

       ③localFilePath 上传文件的地址

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值