1

我正在使用 Flowjs 及其 ng-flow 指令以 NodeJS 作为后端上传文件。当我尝试上传文件时,仅上传 tem 文件夹中的文件,但请注意任何类型的文件,如 JPG 或 PNG。(flow-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;
  }]);

节点

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

1 回答 1

1

重新组装所有块很容易,只需调用以下代码:

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

就是这样!

但另一个问题是,什么时候应该调用这个方法?也许当所有块都上传并出现在 tmp 文件夹中时。

但是重复调用 done 还有另一个问题。一旦所有块都存在,这可以通过创建和锁定文件来解决。然后打电话

    r.write(identifier, stream);

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

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

希望这会有所帮助,我希望有人可以通过这些修复来协作和更新 node.js 示例。

于 2014-05-04T15:40:30.647 回答