我想使用绝对FTP URL下载文件,如ftp://host:port/dir/file.extension
我试过node-libcurl, wget, wget-improved, request
。所有人都失败了,因为协议必须是HTTP或HTTPS。
有可用于Node的FTP客户端(在npmjs上可用)。但是,根据他们的文档,他们需要创建到FTP服务器的连接,更改目录,然后下载它。
有什么简单的解决方案吗?
发布于 2016-08-05 17:43:31
我将在这里概述一个简单的方法(并且没有完整的代码解决方案!)。FTP基于TCP,具有简单的人类可读协议。要从FTP服务器获取文件,您需要执行以下操作:
net.Socket
socket.write
和socket.on('data')
与服务器通信以发送数据和读取数据this blog post中提供了用于简单文件检索的FTPs协议示例,可总结为:
使用net.Socket.connect
net.Socket.connect
PASS
CWD
PASV
的端口上打开另一个套接字
发布于 2016-09-22 23:56:48
你可以使用node-libcurl,我不知道你是怎么做到的,但这里有一些有效的代码。
var Curl = require( 'node-libcurl' ).Curl,
Easy = require( 'node-libcurl' ).Easy,
path = require( 'path' ),
fs = require( 'fs' );
var handle = new Easy(),
url = 'ftp://speedtest.tele2.net/1MB.zip',
// Download file to the path given as first argument
// or to a file named 1MB.zip on current dir
fileOutPath = process.argv[2] || path.join( process.cwd(), '1MB.zip' ),
fileOut = fs.openSync( fileOutPath, 'w+' );
handle.setOpt( Curl.option.URL, url );
handle.setOpt( Curl.option.WRITEFUNCTION, function( buff, nmemb, size ) {
var written = 0;
if ( fileOut ) {
written = fs.writeSync( fileOut, buff, 0, nmemb * size );
}
return written;
});
handle.perform();
fs.closeSync( fileOut );
存储库当前有一个example显示如何使用通配符匹配下载文件,我只是更改了URL以直接指向该文件,并删除了WILDCARDMATCH
和CHUNK_*_FUNCTION
选项。
https://stackoverflow.com/questions/38780205
复制相似问题