[英]Node.js download file from public FTP
嗨,我正在尝试编写一个 node.js 服务来从公共 FTP 下载文件。 我可以手动做的是简单地将 ftp url 粘贴到浏览器并按 Enter。 然后文件将被下载。 我不知道 ftp 的域、用户名或密码。 一个例子是这样的:
我尝试使用节点请求,但它不接受 ftp 协议。 有没有人知道完成这项任务的好工具?
我在 node 中构建了一个 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> </td>
<td align="right">-</td>
<td> </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> </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)
我认为这是你想要利用的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.