表单在inputtypes=文件中没有select文件提交时正在计时

有一个简单的forms,如下面的js小提琴http://jsfiddle.net/UdugW/我张贴一些表单数据到我的node.js应用程序(基于sails.js)。 这是来自product型号的控制器function之一:

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

我没有select上传图片时出现问题,那么我的POST请求就会超时。 任何想法,为什么这可能发生?

9/10与node.js应用程序,当东西超时,你可能忘了closures套接字(至less在我的经验)。 在你的情况下,没有什么不同:-)

这是问题:你有这个if语句

 if( req.files && req.files.product_image){ 

但是这个可怜的家伙没有一个匹配的else语句:-(所以如果这个条件不是真的,那么什么都不会发生,执行基本上就结束了,浏览器仍然在等待…永远。

只要在里面添加一个res.end()就可以了。

所以像这样的东西

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