Flowjs file upload - AngularJS and Node

时间秒杀一切 提交于 2019-12-08 08:53:56

问题


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

回答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.



来源:https://stackoverflow.com/questions/23384222/flowjs-file-upload-angularjs-and-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!