2014-04-30 236 views
1

我正在使用Flowjs及其ng-flow指令以NodeJS作爲後端上載文件。當我嘗試上傳文件時,只會上傳TEM文件夾中的文件,但它會記錄任何類型的文件,如JPG或PNG。 (流135601-juicy_gustinektar02l_10185jpg.1)。這裏是代碼:Flowjs文件上傳 - AngularJS和節點

ANGULARJS

app.config(['flowFactoryProvider', function (flowFactoryProvider) { 
    flowFactoryProvider.defaults = { 
     target: 'http://localhost:8086/api/upload/', 
     permanentErrors: [500, 501], 
     maxChunkRetries: 1, 
     chunkRetryInterval: 5000, 
     simultaneousUploads: 1 
    }; 
    flowFactoryProvider.on('catchAll', function (event) { 
     console.log('catchAll', arguments); 
    }); 
    // Can be used with different implementations of Flow.js 
    //flowFactoryProvider.factory = fustyFlowFactory; 
    }]); 

的NodeJS

// Handle uploads through Flow.js 
app.post('/api/upload', function(req, res){ 
    flow.post(req, function(status, filename, original_filename, identifier){ 
    console.log('POST', status, original_filename, identifier); 

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

// Handle cross-domain requests 
// NOTE: Uncomment this funciton to enable cross-domain request. 
/* 
    app.options('/upload', function(req, res){ 
    console.log('OPTIONS'); 
    res.send(true, { 
    'Access-Control-Allow-Origin': '*' 
    }, 200); 
    }); 
*/ 

// Handle status checks on chunks through Flow.js 
app.get('/api/upload', function(req, res){ 
    flow.get(req, function(status, filename, original_filename, identifier){ 
    console.log('GET', status); 
    res.send(200, (status == 'found' ? 200 : 404)); 
    }); 
}); 

回答

1

重新組裝所有塊很容易,只要把這個:

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

,就是這樣!

但是其他問題是,何時應該調用這個方法? 也許當所有的塊上傳並出現在tmp文件夾。

但是,重複調用done還有另一個問題。 一旦所有塊存在,就可以通過創建和鎖定文件來解決這個問題。 然後調用

r.write(identifier, stream); 

然後清除所有塊,解除鎖定並關閉文件。

同approuch在PHP服務器端庫完成:https://github.com/flowjs/flow-php-server/blob/master/src/Flow/File.php#L102

希望這會有所幫助,我希望有人能協作和更新的node.js與這些修補程序樣本。

+1

'r'應該是什麼? – chovy

+0

我對此也有點困惑。我明白它應該做什麼,但爲什麼這不包括在例子中呢? – flashpunk

+0

此答案中的代碼來自[flow-node.js]中的註釋(https://github.com/flowjs/flow.js/blob/master/samples/Node.js/flow-node.js# L143)。答案是從[這裏]複製/粘貼(https://github.com/flowjs/flow.js/issues/17)。我相信它最初的寫法不正確,因爲'r'沒有意義,我相信它應該是'flow',如果這就是你命名的flow-node.js模塊。所以它應該是: 'var stream = fs.createWriteStream(filename); flow.write(identifier,stream);' – cleversprocket