【问题标题】:Node.js download file from public FTPNode.js 从公共 FTP 下载文件
【发布时间】:2017-05-04 17:07:29
【问题描述】:

您好,我正在尝试编写一个 node.js 服务来从公共 FTP 下载文件。我可以手动做的是简单地将 ftp url 粘贴到浏览器并按 Enter。然后文件将被下载。我不知道 ftp 的域、用户名或密码。一个例子是这样的:

ftp://ftp.agrc.utah.gov/UtahSGID_Vector/UTM12_NAD83/TRANSPORTATION/UnpackagedData/Roads/_Statewide/Roads_shp.zip

我尝试使用节点请求,但它不接受 ftp 协议。有谁知道完成这项任务的好工具吗?

【问题讨论】:

  • 你搜索过谷歌吗?很多包。
  • 是的,有很多针对 node.js + ftp 的包。但是,它们是使用域、用户名和密码的一般连接方法。我正在寻找的是不需要任何信息的东西。相反,它应该使用一个简单的公共 ftp url。

标签: javascript node.js ftp


【解决方案1】:

我正在节点中构建一个 ftp 代理,所以我必须这样做。我做了一个返回文件的函数,如果文件是目录,则返回目录的索引,也许对某人有用。

它还允许用户名和密码 ftp://user:pass@like.this/

var ftp = require('ftp');
var uri = require('lil-uri')
function streamToString(stream) {
  const chunks = []
  return new Promise((resolve, reject) => {
    stream.on('data', chunk => chunks.push(chunk))
    stream.on('error', reject)
    stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
  })
}
async function getftpfile(url) {
  var c = new ftp();
  return await new Promise((resolve, reject) => {
    c.on('ready', function() {
      var p = decodeURI(uri(url).path());
      if (p[p.length - 1] == "/") {
        c.list(p, function(err, list) {
          if (err) throw err;
          //console.dir(list);
          function getlast(str) {
            var parts = encodeURI(str).split("/")
            return parts.pop() || parts.pop()
          }
          c.end();
          resolve(`<!DOCTYPE html>
<html>
<head>
    <title>Index of ${p}</title>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <meta content="width=640, maximum-scale=4, initial-scale=1.0" name="viewport">
    <meta content="yes" name="apple-mobile-web-app-capable">
</head>
<body>
    <h1>Index of ${p}</h1>
    <table>
        <tr>
            <th valign="top"><img alt="[ICO]" src="http://www.xray.mpe.mpg.de/icons/apache/blank.gif"></th>
            <th>Name</th>
            <th>Last modified</th>
            <th>Size</th>
            <th>Description</th>
        </tr>
        <tr>
            <th colspan="5">
                <hr>
            </th>
        </tr>
        <tr>
            <td valign="top"><img alt="[PARENTDIR]" src="http://www.xray.mpe.mpg.de/icons/apache/back.gif"></td>
            <td><a href="..">Parent Directory</a></td>
            <td>&nbsp;</td>
            <td align="right">-</td>
            <td>&nbsp;</td>
        </tr>` +
            list.map(a => `     <tr>
            <td valign="top"><img alt="${a.type=="d"?"[DIR]":"[TXT]"}" src="http://www.xray.mpe.mpg.de/icons/apache/${a.type=="d"?"folder.gif":"text.gif"}"></td>
            <td>
                <a href="${getlast(a.name)+(a.type=="d"?"/":"")}">${a.name}</a>
            </td>
            <td align="right">${a.date.toDateString()}</td>
            <td align="right">${a.size}</td>
            <td>&nbsp;</td>
        </tr>`).join('\n') + `<tr>
            <th colspan="5">
                <hr>
            </th>
        </tr>
    </table>
    <address>
        Index made by hand :/<!--yeah this was from apache, why do you ask?-->
    </address>
</body>
</html>`)
        });
      } else {
        c.get(p, function(err, stream) {
          if (err) {
            throw err
          };
          stream.once('close', function() {
            c.end();
          });
          resolve(streamToString(stream))
        });
      }
    });

    c.connect(uri(url).parts);
  })
}
//Finally get the file that you want.
getftpfile("ftp://ftp.fau.de/apache/README.html").then(console.log)

【讨论】:

    【解决方案2】:

    我认为this 是您想要使用的东西。

    【讨论】:

    • 我检查了这个。它似乎是最流行的 ftp 模块。当我想与它建立连接时,我需要提供域、端口、用户名和密码,对吗?正如我所提到的,我正在寻找的是只需要 url 的东西。
    • @zhangjinzhou 取决于ftp服务器,但是如果是public的话应该可以传空字符串。
    • 你有这方面的例子吗?我所看到的都需要主机。
    • 想通了!谢谢!
    猜你喜欢
    • 2015-10-21
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    • 2017-11-10
    • 1970-01-01
    • 2021-10-10
    相关资源
    最近更新 更多