在节点/快速服务器上上传flow.js后重新组合二进制文件

时间:2014-05-03 19:48:38

标签: node.js file-upload asyncfileupload flow-js

我无法弄清楚如何将flow.js库与节点后端一起使用,并将我的代码基于flow.js github上的示例。

我正在获取blob文件,但是我没有在上传完成后构建二进制文件。最后的结果没有被触发或我的路线错误:

  app.get('/download/:identifier', function(req, res){
    console.log('we writin')
    flow.write(req.params.identifier, res);
  });

任何人都有这方面的经验可能会像一百万stackoverflow pts一样,因为这在使用node.js和flow.js时似乎是一个常见的问题,这里还有另外两个未解决的问题:

Flowjs file upload - AngularJS and Node Reassembling file chunks produced in a multi-part upload

3 个答案:

答案 0 :(得分:8)

我发现了一种有效但可能不是理想方法的方法。

如果flow.writeflow.poststatus,我会在done中呼叫currentTestChunk > numberOfChunks。我做的不仅仅是检查,因为有时flow.post发送status done不止一次here

编辑我在创建文件后添加了清理块的方法。

flow.post(req, function(status, filename, original_filename, identifier, currentTestChunk, numberOfChunks) {
        console.log('POST', status, original_filename, identifier);
        res.send(200);
        if (status === 'done' && currentTestChunk > numberOfChunks) {
            var stream = fs.createWriteStream('./tmp/' + filename);
            //EDIT: I removed options {end: true} because it isn't needed
            //and added {onDone: flow.clean} to remove the chunks after writing
            //the file.
            flow.write(identifier, stream, { onDone: flow.clean });            
        }            
    })

我必须修改flow.post的回调才能发送currentTestChunknumberOfChunks

文件:flow-node.js

$.post = function(req, callback){

//There's some codez here that we can overlook...

  fs.rename(files[$.fileParameterName].path, chunkFilename, function(){

    // Do we have all the chunks?
    var currentTestChunk = 1;
    var numberOfChunks = Math.max(Math.floor(totalSize/(chunkSize*1.0)), 1);
    var testChunkExists = function(){
          fs.exists(getChunkFilename(currentTestChunk, identifier), function(exists){
            if(exists){
              currentTestChunk++;
              if(currentTestChunk>numberOfChunks) {

                //Add currentTestChunk and numberOfChunks to the callback

                callback('done', filename, original_filename, identifier, currentTestChunk, numberOfChunks);
              } else {
                // Recursion
                testChunkExists();
              }
            } else {

              //Add currentTestChunk and numberOfChunks to the callback

              callback('partly_done', filename, original_filename, identifier, currentTestChunk, numberOfChunks);
            }
          });
        }
    testChunkExists();
  });
} else {
      callback(validation, filename, original_filename, identifier);
}

}

如果要删除块,请在flow.write中使用onDone调用flow.clean。

  $.write = function(identifier, writableStream, options) {
      options = options || {};
      options.end = (typeof options['end'] == 'undefined' ? true : options['end']);

      // Iterate over each chunk
      var pipeChunk = function(number) {

          var chunkFilename = getChunkFilename(number, identifier);
          fs.exists(chunkFilename, function(exists) {

              if (exists) {
                  // If the chunk with the current number exists,
                  // then create a ReadStream from the file
                  // and pipe it to the specified writableStream.
                  var sourceStream = fs.createReadStream(chunkFilename);
                  sourceStream.pipe(writableStream, {
                      end: false
                  });
                  sourceStream.on('end', function() {
                      // When the chunk is fully streamed,
                      // jump to the next one
                      pipeChunk(number + 1);
                  });
              } else {
                  // When all the chunks have been piped, end the stream
                  if (options.end) {
                          writableStream.end();
                      }

                  //Options.onDone contains flow.clean so here I'm deleting all the chunked files.

                  if (options.onDone) {
                      options.onDone(identifier);
                  }
              }
          });
      }
      pipeChunk(1);
  }

答案 1 :(得分:3)

好的,所以我一直在努力解决这个问题并提出这个问题,希望有人会开始......

exports.post = function (req, res, next) {

    flow.post(req, function(status, filename, original_filename, identifier) {

        console.log('status: '+ status, filename, original_filename, identifier);

        if(status==='done'){

            var s = fs.createWriteStream('./uploads/' + filename);
            s.on('finish', function() {

                res.send(200, {
                    // NOTE: Uncomment this funciton to enable cross-domain request.
                    //'Access-Control-Allow-Origin': '*'
                });

            });

            flow.write(identifier, s, {end: true});
        }

    });

};

答案 2 :(得分:0)

我发出了一个pull请求,它将重组逻辑添加到Flow.js的Node示例中。请参阅https://github.com/flowjs/flow.js/pull/71https://github.com/flowjs/flow.js/issues/17