2014-05-03 14 views
10

我無法弄清楚如何將flow.js庫與節點後端一起使用,並將我的代碼放在github的flow.js樣本上。在flow.js在節點/快遞服務器上上傳之後重新組裝二進制文件

我得到了blob文件,但是我沒有在上傳完成後創建二進制文件。最終得到的是沒有得到觸發或我的路線是錯誤的:

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

任何人有任何這方面的經驗可以得到像一百萬計算器點,因爲這似乎是一個常見的問題,如何使用Node.js和流量的時候。 js和這裏有另外兩個懸而未決的問題:

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

回答

8

我發現了一種可行的方法,但可能不是理想的方法。

在這裏,我在flow.post調用flow.write如果statusdonecurrentTestChunk > 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

文件:流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); 
} 

}

與onDone flow.write通話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); 
    } 
+0

太棒了! 任何機會,你可以提交PR到flow.Js github回購?這裏是一個鏈接:https://github.com/flowjs/flow.js/issues/17#issuecomment-49737531 – flashpunk

+0

嘿@flashpunk,當我有機會,我會研究。 – cleversprocket

+0

我沒有看到在第一次檢查後立即在回調中重複檢查「if(currentTestChunk> numberOfChunks)」如何可以幫助修復多次觸發完成狀態的問題。 –

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

    }); 

}; 
相關問題