【问题标题】:Nodejs streaming upload multiple filesNodejs流式上传多个文件
【发布时间】:2014-08-24 17:15:08
【问题描述】:

我正在开发一个命令行应用程序,它应该采用一组文件名, 进行转换操作(文本文件、电子表格等有效负载必须重写为 JSON 对象),并将结果发送到端点 API (api.example.com)。 我正在考虑顺序读取,并将结果通过管道传输到 -http 或 -request 的实例, 但不知道从哪里开始。您是否有任何替代方案或策略来解决类似的问题?

任何算法,或指向一篇文章或类似的问题在这里都将受到高度赞赏。 谢谢。

更新1。我在这个谷歌群组https://groups.google.com/forum/#!topic/nodejs/_42VJGc9xJ4找到了一个可能有帮助的链接

跟踪最终解决方案:

var request = require('request');
var file = fs.createReadStream(path)
      .pipe(request.put({url: url, headers:{'Content-Length': fileSize}}, function(err, res, body){
        if(err) {
          console.log('error', err);
        } else {
          console.log('status', res.statusCode);
          if(res.statusCode === 200) {
            console.log('success'); 
          }
        }
      }));

剩下的问题是如何使这对“n”个文件起作用,以防“n”很高 - 100 个文本文件或更多。

【问题讨论】:

    标签: node.js file stream node-request


    【解决方案1】:

    在尝试和错误之后,我用事件解决了这个问题,我粘贴了答案,以防其他人正在努力解决类似的问题。

    var EventEmitter = require('events').EventEmitter;
    var request        = require('request');
    var util          = require('util');
    
    
    function Tester(){
      EventEmitter.call(this);
      this.files = ['http://google.ca', 'http://google.com', 'http://google.us'];
    
    
    
    }
    
    util.inherits( Tester, EventEmitter );
    
    Tester.prototype.run = function(){
      //referencing this to be used with the kids down-here  
      var self = this;
      if( !this.files.length ) { console.log("Cannot run again .... "); return false; }
      request({ url : this.files.shift()}, function( err, res, body ) {
        console.log( err, res.statusCode, body.length, " --- Running the test --- remaining files ", self.files );
        if( !self.files.length ) self.emit( "stop" );
        else self.emit( "next" , self.files );
      });
    };
    
    //creating a new instance of the tester class
    var tester = new Tester();
      tester.on("next", function(data){
           //@todo --- wait a couple of ms not to overload the server.
           //re-run each time I got this event
           tester.run();
      });
      tester.on("stop", function(data){
        console.log("Got stop command --- Good bye!");
      });
      //initialize the first run
      tester.run();
      //graceful shutdown -- supporting windows as well 
      //@link http://stackoverflow.com/questions/10021373/what-is-the-windows-equivalent-of-process-onsigint-in-node-js
      if (process.platform === "win32") {
        require("readline").createInterface({
            input: process.stdin,
            output: process.stdout
        }).on("SIGINT", function () {
            process.emit("SIGINT");
        });
    }
    
    process.on("SIGINT", function () {
        // graceful shutdown
        process.exit();
    });
    
    
    
    console.log('Started the application ... ');
    

    注意:

    • 如需快速测试,请here is the runnable

    • 我使用 get 进行快速测试,但也可以使用 post/put。 希望对你有帮助,欢迎留言。谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      相关资源
      最近更新 更多