[英]Form is timeouting on submitting without selected file in input type=file
Having a simple form as in the following js Fiddle http://jsfiddle.net/UdugW/ I am POSTing some form data to my node.js application (based on sails.js). 有一个简单的形式,如下面的js小提琴http://jsfiddle.net/UdugW/我正在将一些表单数据发布到我的node.js应用程序(基于sails.js)。 This is one of the controller functions from
product
model: 这是
product
型号的控制器功能之一:
image_upload: function(req, res) {
if (req.method.toLowerCase() == 'post') {
console.log("request: " + util.inspect(req.body));
if( req.files && req.files.product_image){
fs.readFile(req.files.product_image.path, function (err, data) {
var imageName = req.files.product_image.name;
if(!imageName){
console.log("There was an error with image upload : " + util.index(req.files.product_image));
res.redirect("/product/new_product");
res.end();
} else {
var newPath = UPLOAD_PATH + imageName;
/// write file to uploads/fullsize folder
fs.writeFile(newPath, data, function (err) {
res.writeHead(200, {'content-type': 'text/plain'});
res.end();
return;
});
}
});
}
}else if (req.body) {
console.log("product_name: " + product_name);
console.log("product_price: " + product_price);
console.log("product_description: " + product_description);
res.writeHead(200, {'content-type': 'text/plain'});
res.end();
}
else{
console.log("request err");
res.writeHead(500, {'content-type': 'text/plain'});
res.end();
}
},
I have a problem when I do not select an image for upload, then my POST request is timing out. 我没有选择要上传的图像时出现问题,然后我的POST请求超时。 Any ideas why this may happen ?
任何想法为什么会这样?
9/10 with node.js app, when something is timing out, chances are you forgot to close the socket (at least in my experience). 9月10日使用node.js应用程序,当某些内容超时时,您可能忘记关闭套接字(至少根据我的经验)。 and in your case it's no different :-)
在你的情况下它没有什么不同:-)
here is the problem: you have this if-statement 这是问题:你有这个if语句
if( req.files && req.files.product_image){
but the poor guy doesn't have a matching else-statement :-( so if that condition is not true... well, nothing happens. execution basically just ends and the browser is left waiting... for ever. 但是穷人没有匹配的else语句:-(所以如果那个条件不正确......好吧,没有任何事情发生。执行基本上就结束了,浏览器就等待了......永远。
just adding an else-statment with a res.end()
inside of it and you should be good. 只需在其中添加一个带有
res.end()
的else-statment就可以了。
So something like this 所以这样的事情
if( req.files && req.files.product_image){
//all the stuff you already have for when it matches
}else{
console.log("No files were included in the post");
res.end();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.