How to download file over FTP in NodeJS?

只谈情不闲聊 提交于 2021-02-08 10:22:18

问题


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

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

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?


回答1:


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. 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

An example of FTPs protocol for a simple file retrieval is provided in this blog post and can be summarized as follows:

  1. Connect to server using net.Socket.connect
  2. Set user with USER command
  3. Authenticate with PASS
  4. Go to desired directory using CWD
  5. Change to passive mode using PASV
  6. Read server reply to find out IP and port to connect to in order to fetch the file
  7. Open another socket on IP and port of previous step
  8. Voilà!



回答2:


You can use node-libcurl, I don't know exactly how you did it, but here is some working code.

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 );

The repository currently has one example showing how to download a file using wildcard matching, I just changed the URL to point directly at the file, and removed the WILDCARDMATCH and CHUNK_*_FUNCTION options.



来源:https://stackoverflow.com/questions/38780205/how-to-download-file-over-ftp-in-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!