如何在节点中使用easy-ftp上传文件?

时间:2018-02-04 19:32:42

标签: node.js sftp

我正在尝试使用node和easy-ftp将文件上传到我的托管服务器。

我尝试使用以下代码:

var EasyFtp = require ("easy-ftp");
var ftp = new EasyFtp();

var config = {

    host:'homexxxxx.1and1-data.host',
    type:'SFTP',
    port:'22',
    username:'u90xxxx',
    password:"mypass"
};
ftp.connect(config);
ftp.upload("/test/test.txt", "/test.txt", function(err){
    if (err) throw err;
    ftp.close();
});

没有错误消息但没有上传文件

我尝试使用promises

const EasyFTP = require('easy-ftp-extra')
const ftp = new EasyFTP()
const config = {
        host:'homexxxxx.1and1-data.host',
        type:'SFTP',
        port:'22',
        username:'u90xxxx',
        password:"mypass"
    };
ftp.connect(config);

ftp.upload('/test.txt', '/test.txt')
  .then(console.log)
  .catch(console.error)
ftp.upload()

同样发布。没有上传文件。节点控制台中没有错误。

配置与filezilla中用于传输文件的配置相同。 SFTP协议。一切都与filezilla一起运作良好。 我做错了什么?

1 个答案:

答案 0 :(得分:0)

看起来这里可能有路径问题。

“/测试/ test.txt的”

指定的路径将尝试从根文件夹中获取文件,如“c:\ test \ test.txt”。

假设您希望从项目文件夹中获取文件,请尝试以下路径: “./test/test.txt”

您的代码中的其他内容与我和我的作品完全相同。

相关问题