user3163213
user3163213

Reputation: 701

uploading file to ftp server in node.js

I am trying to upload a file on ftp server using node.js as below-

I am using library- 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 . 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?

Upvotes: 19

Views: 27646

Answers (3)

Can Bahadır Ceylan
Can Bahadır Ceylan

Reputation: 1

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');
        }
    });
});

Upvotes: 0

Dickens
Dickens

Reputation: 33

The raw FTP accepts no parameters and returns the farewell message from the server. Embed raw FTP function in the FTP GET method

We can use raw FTP commands directly as well. In this case, we use FTP 'QUIT' method, which accepts no parameters and returns the farewell message from the server

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

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

Upvotes: 0

Valera
Valera

Reputation: 2913

If the debug mode is on, the jsftp instance will emit jsftp_debug events.

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));
});

Upvotes: 2

Related Questions