1

I am using Flowjs, and its ng-flow directive to upload file with NodeJS as backend. When i try to upload file, only file in tem folder is uploaded, but it's note any type of file like JPG or PNG. (flow-135601-juicy_gustinektar02l_10185jpg.1). Here is code:

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));
  });
});
Herb Caudill
  • 50,043
  • 39
  • 124
  • 173
Sysrq147
  • 1,359
  • 4
  • 27
  • 49

1 Answers1

1

Reassembling all chunks is easy, just call this:

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

And that is it!

But other question is, when this method should be called? Maybe when all chunks are uploaded and present at tmp folder.

But there is another issue with duplicate calls of the done. This can be solved by creating and locking the file, once all chunks exists. Then call

    r.write(identifier, stream);

Then clean all chunks, release the lock and close the file.

Same approuch is done in php server side library: https://github.com/flowjs/flow-php-server/blob/master/src/Flow/File.php#L102

Hope this helps, and I hope someone could collaborate and update node.js sample with these fixes.

user3499275
  • 207
  • 3
  • 9
  • 1
    what is `r` supposed to be? – chovy Jul 13 '14 at 04:30
  • I'm a bit confused about this as well. I see what it is supposed to do, but why isn't this included in the examples? – flashpunk Jul 20 '14 at 20:53
  • The code in this answer is pulled from the comments in [flow-node.js](https://github.com/flowjs/flow.js/blob/master/samples/Node.js/flow-node.js#L143). The answer is copy/pasted from [here](https://github.com/flowjs/flow.js/issues/17). I believe it was originally written incorrectly because `r` has no meaning and I believe it should be `flow`, if that is what you've named the flow-node.js module. So it should be: `var stream = fs.createWriteStream(filename); flow.write(identifier, stream);` – cleversprocket Jul 25 '14 at 15:33
  • Check out my answer [here](https://stackoverflow.com/questions/23449065/reassemble-binary-after-flow-js-upload-on-node-express-server/24948858#24948858) for a solution that works. – cleversprocket Jul 25 '14 at 15:38