jsftp - TypeError: createConnection is not a function

jsftp - TypeError: createConnection is not a function

我刚刚使用 client-ftp and was told that package was outdated and was the source of my original problem. As suggested I moved to an package that is still being maintained, which is jsftp. However, I am still having similiar issues. For reference my previous question can be found 发布了一个最近的问题。我仍然会重申我的问题,因为它略有变化。

我是 Angular/Node 的新手,正在尝试连接到 ftp 服务器。

我正在这样的前端创建一个按钮: <button class="btn btn-primary" (click)="downloadFile()"><i class="fa fa-file-photo-o"></i> Download Screenshot</button>

我要下载的新实现是这样的:

downloadFile(){
    console.log('Connecting to sftp...');
    var jsftp = require("jsftp");

    var ftp = new jsftp({
        host: 'localhost',
        port: 22,
        user: 'anonymous',
        pass: 'anonymous'
    });

    console.log('Downloading Screenshot...');
}

我知道我并没有尝试下载任何东西,但现在我只是先尝试​​连接。问题是当单击事件被触发时,我收到一条错误消息 TypeError: createConnection is not a function。我不知道我做错了什么。 createConnection 函数未在任何地方被调用。我正在做文档所说的,其他资源似乎也表明相同,例如 here

节点版本是:v8.11.3 - 可能太过时了?

您标记打字稿,大概是因为 TypeError,这是一个 javascript,而不是打字稿,错误(参见 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_a_function)。

其次,您似乎混淆了 javascript 在 NodeJS 上是后端,在浏览器中是前端。你不能在浏览器中 运行 jsftp,所以你的方法需要改变。

如果您的目的是让用户在单击 link 时从他们的浏览器下载 ftp 文件,您应该能够直接在 href 中 link 到它, 没有任何 javascript。查看此问题的答案以获取更多信息:Is it possible to download file from FTP using Javascript?