文件上传期间的 ETIMEOUT。风帆JS

ETIMEOUT during file upload. SAILS JS

我有一个带有文件上传字段的表单。如果我上传文件,它工作正常。但是当我提交没有文件的表单时,它会重定向到预期的页面并给出 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 但也给出了错误。

使用 req.file('avatar') 将尝试检查已上传但尚未上传的文件,因此使用 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."});
   }
});