文件上传期间的ETIMEOUT。 SAILS JS

时间:2015-01-30 07:38:47

标签: javascript file-upload sails.js

我有一个包含文件上传字段的表单。如果我上传文件,它的工作正常。但是当我提交带有out文件的表单时,它会重定向到预期的页面,并且也会出现ETIMEOUT错误。

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: ETIMEOUT: An Upstream (`NOOP_avatar`) timed out waiting
iles were sent after waiting 500ms.
    at null.<anonymous> (C:\xampp\htdocs\fileuploadtest\node_m
ndalone\Upstream\Upstream.js:58:15)

我的代码看起来像这样:

upload_image: function (req, res) {
    var uploadPath = '../../assets/images/uploads/';
    console.log(req.file('avatar')._files);
    if(req.file('avatar')._files.length > 0){
      req.file('avatar').upload({
          saveAs: function(file, cb) {
            cb(null, file.filename);
          },
          dirname: uploadPath
        }, function whenDone(err, uploadedFiles) {
          if (err){
            return res.serverError(err);
          } else{
            return res.view('showimage', {file:uploadedFiles});
          }
        });
    }else{
      console.log('no data');
       return res.view('showimage');
    }
  },

它重定向到showimage但也出错。

1 个答案:

答案 0 :(得分:1)

使用req.file(&#39; avatar&#39;)将尝试检查尚未上传的文件,因此使用if条件会给您一个错误。使用.upload()函数后尝试使用if条件。

req.file('avatar').upload({
  saveAs: function(file, cb) {
    cb(null, file.filename);
  },
  dirname: uploadPath
}, function whenDone(err, uploadedFiles) {
  if (err){
    return res.serverError(err);
  } 
  if(uploadedFiles.length!=0){
    return res.view('showimage', {file:uploadedFiles});
  }else{
   return res.view('showimage', {file:"No files uploaded."});
   }
});