Reputation: 8234
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 ?
Upvotes: 2
Views: 2324
Reputation: 8234
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
Upvotes: 5