gpt4 book ai didi

node.js - 船长 : how to check if file was uploaded or not

转载 作者:搜寻专家 更新时间:2023-11-01 00:24:10 28 4
gpt4 key购买 nike

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

我的代码是这样的:

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
}

没有上传图片时出现以下错误:

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 条件总是被评估为真。如何检查用户是否没有上传任何文件?

最佳答案

将我的代码更改为

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

现在它工作正常

关于node.js - 船长 : how to check if file was uploaded or not,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29296136/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com