我正在使用优秀的 flow.js 库来处理文件上传。这是一个可恢复的 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 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;
}
}