简体   繁体   English

将文件上传到 node.js 中的 ftp 服务器

[英]uploading file to ftp server in node.js

I am trying to upload a file on ftp server using node.js as below-我正在尝试使用 node.js 在 ftp 服务器上上传文件,如下所示 -

I am using library- https://github.com/sergi/jsftp我正在使用库 - https://github.com/sergi/jsftp

var fs = require('fs');
var Ftp = new JSFtp({
    host: "ftp.some.net",
    port: 21, // defaults to 21
    user: "username", // defaults to "anonymous"
    pass: "pass",
    debugMode: true // defaults to "@anonymous"
});

Uploading file-上传文件-

exports.UploadToFtP = function (req, res) {
     Ftp.put('public/Test.html', '/Test/index.html', function (err) {
            if (!err)
                res.send(200);
            else
                res.send(err);
        });
};

I tried uploading file with this method above and it responds me back with 200 OK .我尝试使用上述方法上传文件,它以200 OK回复我。 But I get no file on server.但是我在服务器上没有得到任何文件。 Is this has to do something with connection time out of server ?这是否与服务器的连接时间有关? Why this is not writing file on server?为什么这不在服务器上写入文件?

If the debug mode is on, the jsftp instance will emit jsftp_debug events.如果调试模式打开,jsftp 实例将发出 jsftp_debug 事件。

In order to react to print all debug events, we would listen to the debug messages like this:为了对打印所有调试事件做出反应,我们将听取如下调试消息:

Ftp.on('jsftp_debug', function(eventType, data) {
    console.log('DEBUG: ', eventType);
    console.log(JSON.stringify(data, null, 2));
});

The raw FTP accepts no parameters and returns the farewell message from the server.原始 FTP 不接受任何参数并从服务器返回告别消息。 Embed raw FTP function in the FTP GET method在 FTP GET 方法中嵌入原始 FTP 函数

We can use raw FTP commands directly as well.我们也可以直接使用原始 FTP 命令。 In this case, we use FTP 'QUIT' method, which accepts no parameters and returns the farewell message from the server在这种情况下,我们使用 FTP 'QUIT' 方法,该方法不接受任何参数并从服务器返回告别消息

ftp.raw.quit(function(err, res) {
    if (err)
        return console.error(err);

    console.log("FTP session finalized! See you soon!");
});

The file needs to be converted to bytes first.该文件需要先转换为字节。

var fs = require('fs');

fs.readFile('example.txt', function (err, data ) {
    Ftp.put(data, 'example.txt', function (err) {
        if (!err) {
            console.log('OK');
        } else {
            console.log('ERR');
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM