首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sails skipper:如何检查文件是否已上传

sails skipper:如何检查文件是否已上传
EN

Stack Overflow用户
提问于 2015-03-27 16:21:56
回答 1查看 2.1K关注 0票数 2

我使用的是sails 0.10.5,它使用skipper进行文件上传。现在我有了一个表单,用户可以在其中上传文件。但是,文件上传不是强制的。在后台,我需要检查是否上传了文件。

我的代码是这样的:

代码语言:javascript
复制
if(req.file('img')){
  req.file('img').upload({
    maxBytes: 10000000
  }, function (err, uploadedFiles){
    if(err){
      return res.json(500, err);
    }
    else if(uploadedFiles.length === 0){
      return res.json(500, {"error": "no file uploaded"});
    }
    else{
        // do something with image
    }
  });
}
else {
    // proceed without image
}

当没有上传图片时,我得到了以下错误:

代码语言:javascript
复制
Error: EMAXBUFFER: An Upstream (`NOOP_img`) timed out before it was plugged into a receiver. It was still unused after waiting 4500ms. You can configure this timeout by changing the `maxTimeToBuffer` option.
    at null.<anonymous> (/home/mandeep/projects/thirstt/node_modules/sails/node_modules/skipper/standalone/Upstream/Upstream.js:86:15)
    at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)

第一个if条件的计算结果总是为true。如何检查用户是否未上传任何文件?

EN

回答 1

Stack Overflow用户

发布于 2015-03-27 16:32:40

将代码更改为

代码语言:javascript
复制
req.file('img').upload({
  maxBytes: 10000000
}, function (err, uploadedFiles){
  if(err){
    return res.json(500, err);
  }
  else if(uploadedFiles.length === 0){
    // proceed without files
  }
  else{
    //  handle uploaded file
  }
});

现在它工作得很好

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29296136

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档