简体   繁体   中英

sails skipper: how to check if file was uploaded or not

I am using sails 0.10.5 which uses skipper for file uploads. Now I have a form where a user can upload a file. However, the file upload is not mandatory. At backend, I need to check if file was uploaded or not.

My code is something like this:

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
}

I am getting the following error when no image is uploaded:

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)

The first if condition is always being evaluated to true. How do I check if user did not upload any file ?

changed my code to

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

and now its working fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM