简体   繁体   English

使用 jsftp 从 Node.js 上传到 FTP

[英]Uploading to FTP from Node.js using jsftp

I'm trying to upload a file to FTP server from node js using jsftp我正在尝试使用jsftp从节点 js 将文件上传到 FTP 服务器

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.创建 ftp 对象没有错误。 I'm getting a 200 response but there's no file uploaded to the FTP server.我收到 200 响应,但没有文件上传到 FTP 服务器。 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.否则,根本不打印“in”。

I have no idea what's gong on here.我不知道这里发生了什么。 Any help is appreciated!任何帮助表示赞赏!

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

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

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