Flowjs文件上传 - AngularJS和Node

时间:2014-04-30 09:19:41

标签: node.js angularjs backend flow-js

我使用Flowjs及其ng-flow指令以NodeJS作为后端上传文件。当我尝试上传文件时,只上传了tem文件夹中的文件,但它注意到任何类型的文件,如JPG或PNG。 (流135601-juicy_gustinektar02l_10185jpg.1)。这是代码:

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 个答案:

答案 0 :(得分:1)

重新组装所有块很容易,只需这样称呼:

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

就是这样!

但其他问题是,应该调用此方法吗? 也许当所有的块都被上传并出现在tmp文件夹中时。

但是完成的重复调用存在另一个问题。 一旦存在所有块,就可以通过创建和锁定文件来解决这个问题。 然后拨打

    r.write(identifier, stream);

然后清除所有块,释放锁并关闭文件。

在php服务器端库中完成相同的approuch:https://github.com/flowjs/flow-php-server/blob/master/src/Flow/File.php#L102

希望这会有所帮助,我希望有人可以使用这些修补程序协作并更新node.js示例。