Anand Srinivasan
Anand Srinivasan

Reputation: 470

Uploading to FTP from Node.js using jsftp

I'm trying to upload a file to FTP server from node js using jsftp

module.exports =   async function() {

    const jsftp = require("jsftp"); 
    console.log(__dirname)
    const ftp = new jsftp({
        host: "host.name",
        port: 990, 
        user: "user", 
        pass: "pass" 
    });
    ftp.put('/path/to/file.xlsx', '/path/to/remote/file/qwer.xlsx', err => {
                console.log('in')
                if (!err) console.log("File transferred successfully!");
                else console.log(err)
              });
}

There are no errors in creating the ftp object. I'm getting a 200 response but there's no file uploaded to the FTP server. If I make an error in the local path, I can see "Local file doesn't exist" error in the console. Otherwise, the "in" is not not printed at all.

I have no idea what's gong on here. Any help is appreciated!

Upvotes: 2

Views: 1995

Answers (1)

Andy Farrell
Andy Farrell

Reputation: 11

You need to read the file first. So, something like:-

module.exports =   async function() {

    const jsftp = require("jsftp"); 
    const fs = require("fs");
    console.log(__dirname)
    const ftp = new jsftp({
        host: "host.name",
        port: 990, 
        user: "user", 
        pass: "pass" 
    });
    fs.readFile(local, function(err, buffer) {
        if(err) {
            console.error(err);
            callback(err);
        }
        else {
            ftp.put('/path/to/file.xlsx', '/path/to/remote/file/qwer.xlsx', err => {
                console.log('in')
                if (!err){ console.log("File transferred successfully!")};
                else {console.log(err)}
            });
       }
    });
}

Upvotes: 1

Related Questions