在flow.js上传到node / express服务器后,重新组装二进制文件

我无法弄清楚如何在节点后端使用flow.js库,并将我的代码放在flow.js github上的示例中 。

我得到了blob文件,但是在上传完成之后,我并没有构build二进制文件。 最终得到的是没有被触发或我的路线是错误的:

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

任何人有任何经验,可以得到像百万计算器的堆栈,因为这似乎是使用node.js和flow.js的常见问题,这里还有两个未解答的问题:

Flowjsfile upload – AngularJS和Node 重组文件块以多部分上传方式生成

我find了一种方法,但可能不是理想的方法。

在这里,如果status已经done并且currentTestChunk > numberOfChunks则在flow.post调用flow.write 。 我做大于检查,因为有时flow.post发送status done不止一次,如上所述。

编辑:我添加了一个方法来创build文件后清理块。

 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的callback来发送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调用flow.clean与onDone如果你想删除块。

  $.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); } 

好的,我一直在研究这个,想出了这个,希望能有人开始…

 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}); } }); }; 

我已经发出了一个pull请求,为Flow.js的Node实例增加了重组逻辑。 请参阅https://github.com/flowjs/flow.js/pull/71和https://github.com/flowjs/flow.js/issues/17