js实现上传文件到ftp服务器上

上传本地文件到ftp服务器

创建js服务器

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
var Client = require('ftp');
var fs = require('fs');

var c = new Client();
c.connect({
    host: "xxxxx",
    type: "FTP",
    port: 21,
    user: "xxx",
    password: "xxxx",
});

app.use(bodyParser.json());

app.put('/campaigns', (req, res) => {
    let fullpath = __dirname + "/campaigns/" + req.query.name + "11";
    console.log(fullpath);
    c.put(fullpath, `/uploads/${req.query.name}`, function(err) {
        if (err) throw err;
        res.sendStatus(200)
    });
})


const port = process.env.PORT || 5000;
app.listen(port)

module.exports = app;

启动js服务器

npm run start
在这里插入图片描述

启动react画面

npm run start
在这里插入图片描述

画面上传文件到ftp

在这里插入图片描述

ftp服务器的文件在这里插入图片描述

上传文件

在这里插入图片描述

文件上传成功

在这里插入图片描述
上面就是把文件从本地上传到ftp服务器上。

js实现ftp服务器之间传输文件

创建js服务器

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
var Client = require('ftp');
var fs = require('fs');
 
var c = new Client();
c.connect({
    host: "xxxx1",
    type: "FTP",
    port: 21,
    user: "xx1",
    password: "xxx1",
});

var d = new Client();
d.connect({
    host: "xxx2",
    type: "FTP",
    port: 21,
    user: "xx2",
    password: "xxx2",
});

app.use(bodyParser.json());

app.put('/change', (req, res) => {
     const fileName = "/uploads/test33.txt";
     c.passive = true;
     // 打开源FTP上的文件流
     c.get(fileName, (err, stream) => {
         console.log("1");
         if (err) throw err;
         const localPh = "tmp";
         const writeStream = fs.createWriteStream(localPh);
         stream.pipe(writeStream);

         console.log("2");
         stream.on('data',data=>{
            console.log(data.toString());
         })

         writeStream.on('finish',()=>{
            // 写入目标FTP服务器上的同名文件
            d.put(localPh,fileName,(err) => {
                if (err) throw err;
                console.log("3");
                console.log(`${fileName} 文件传输完成`);
                fs.unlinkSync(localPh);
            });
         });
     });
})

const port = process.env.PORT || 5000;
app.listen(port)

module.exports = app;

和上面一样启动服务

在这里插入图片描述

两个ftp服务器的数据

server1
在这里插入图片描述
server2
在这里插入图片描述

画面执行,文件传输

在这里插入图片描述

执行后,两个ftp服务器文件

server1
在这里插入图片描述
server2

到这里两个ftp服务器文件传输结束了。

上面就是用react做的页面,用js做的服务,然后来上传本地文件到ftp服务器,还有两个ftp服务器之间进行数据传送。
希望能对你有所帮助。

  • 25
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值