美文网首页
NodeJS 连接FTP服务器

NodeJS 连接FTP服务器

作者: zhousen05 | 来源:发表于2018-04-26 16:07 被阅读0次

    ftp一般是这样 ftp://帐号:密码@服务器IP(或者域名如:xxx.com):端口号。

    来自https://github.com/mscdex/node-ftp

    先安装 npm install ftp

    var client = require('ftp');

        var fs = require('fs');

        var c = new client();

    c.on('ready', function() {

    //和ftp服务器连接成功后,会进到这里来。接着可以上传下载文件或者做其它事情了。

             c.list(function(err, list) {//Get a directory listing of the current (remote) working directory:

                   if(err) throw err;

                   console.dir(list);

                   c.end();

              });

           c.get('remote file path', function(err, stream) {  //Download remote file  and save it to the local file system:

                  if(err) throwerr;

                  stream.once('close', function() {

                              console.log('download Finish');

                   });

              stream.pipe(fs.createWriteStream('local file path'));

           });

           });

    var connectionProperties = {

        host:'xxx',

        user:'xxx',

        password:'xxx',

        port:xx  //默认是21,这个看自己要连接的端口

        };

       c.connect(connectionProperties);

    相关文章

      网友评论

          本文标题:NodeJS 连接FTP服务器

          本文链接:https://www.haomeiwen.com/subject/hecmlftx.html