重组在多部分上传中生成的文件块

我正在使用优秀的flow.js库来处理file upload。 这是一个可恢复的HTML5上传,在服务器上产生大量的块,必须重新组合。 例如,foo.mov可能会变成

timestamp-foomov.1 timestamp-foomov.2 ... timestamp-foomov.n 

上传正在工作,但我很难重新组合成一个单一的二进制文件。 我从库作者在Github上提供的Node.js服务器示例( https://github.com/flowjs/flow.js/tree/master/samples/Node.js )中获得了以下代码。

  $.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(); if (options.onDone) options.onDone(); } }); } pipeChunk(1); } 

我用下面的路线调用这个代码,并期望它在tmp目录中生成一个重组的二进制文件(这是我保存我的块)。 然而,没有发生。 我错过了什么?

 exports.download = function(req, res, next) { switch(req.method) { case 'GET': var stream = fs.createWriteStream('foobar'); flow.write(req.params.identifier, res); break; } } 

重新组装所有块很容易,只需调用这个:

  var stream = fs.createWriteStream(filename); r.write(identifier, stream); 

就是这样!

但是另一个问题是,何时应该调用这个方法呢? 也许当所有的块上传并出现在tmp文件夹。

但是,重复调用done还有另一个问题。 一旦所有块存在,就可以通过创build和locking文件来解决这个问题。 然后打电话

  r.write(identifier, stream); 

然后清理所有块,释放锁并closures文件。

同样的approuch在php服务器端库中完成: https : //github.com/flowjs/flow-php-server/blob/master/src/Flow/File.php#L102