如何在NodeJS中通过FTP下载文件? [英] How to download file over FTP in NodeJS?

查看:81
本文介绍了如何在NodeJS中通过FTP下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用绝对FTP URL下载文件,例如 ftp://host:port/dir/file.extension

I want to download file using absolute FTP URL, like ftp://host:port/dir/file.extension

我尝试了 node-libcurl,wget,wget改进的请求.所有人都失败了,说协议必须是HTTP或HTTPS.

I've tried node-libcurl, wget, wget-improved, request. All failed saying that the protocol must be either HTTP or HTTPS.

有一些可用于Node的FTP客户端(在npmjs上可用).但是,根据他们的文档,他们需要创建与FTP服务器的连接,更改目录然后下载.

There are FTP clients available for Node (available on npmjs). But, as per their documentation, they require creating a connection to FTP Server, change directory and then download it.

有什么简单的解决方法吗?

Is there any simple solution?

推荐答案

在这里,我将概述一种简单的方法(并且没有完整的代码解决方案!). FTP 基于

I will outline a simple approach here (and no complete solution with code!). FTP is based upon TCP with a simple human readable protocol. In order to fetch a file from an FTP server you need to do the following:

  1. 使用 net.Socket <
  2. 使用 socket.connect 在端口21上连接到FTP服务器
  3. 使用 socket.write 与服务器进行通信发送数据和 socket.on('data') 到读取数据
  1. Create a TCP socket using net.Socket
  2. Use socket.connect to connect to your FTP server on port 21
  3. Communicate with the server using socket.write to send data and socket.on('data') to read data

此博客帖子,可以总结如下:

  1. 使用 net.Socket.connect
  2. 连接到服务器
  3. 使用 USER 命令设置用户
  4. 通过 PASS
  5. 进行身份验证
  6. 使用 CWD
  7. 转到所需目录
  8. 使用 PASV
  9. 更改为被动模式
  10. 阅读服务器回复以找出要连接的IP和端口以获取文件
  11. 在上一步的IP和端口上打开另一个套接字
  12. Voilà!

这篇关于如何在NodeJS中通过FTP下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆