What is the best way to download a big file in NodeJS?

前端 未结 3 2016
梦如初夏
梦如初夏 2021-02-15 08:09

The below server code is working fine for 5GB file using wget http://localhost:11146/base/bigFile.zip but not using client side code.

Serve

3条回答
  •  情深已故
    2021-02-15 08:44

    My version of code .

    const fs = require( 'fs' );
    const request = require( 'request' );
    const progress = require( 'request-progress' );
    const pre = '----';
    const downloadManager = function ( url, filename ) {
        progress( request( url ), {
            throttle: 500
        } ).on( 'progress', function ( state ) {
            process.stdout.write( pre + '' + ( Math.round( state.percent * 100 ) ) + "%" );
        } ).on( 'error', function ( err ) {
            console.log( 'error :( ' + err );
        } ).on( 'end', function () {
            console.log( pre + '100% \n Download Completed' );
        } ).pipe( fs.createWriteStream( filename ) );
    }
    downloadManager( 'http://localhost:4181', 's.zip' );
    

提交回复
热议问题