[英]Form is timeouting on submitting without selected file in input type=file
有一個簡單的形式,如下面的js小提琴http://jsfiddle.net/UdugW/我正在將一些表單數據發布到我的node.js應用程序(基於sails.js)。 這是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();
}
},
我沒有選擇要上傳的圖像時出現問題,然后我的POST請求超時。 任何想法為什么會這樣?
9月10日使用node.js應用程序,當某些內容超時時,您可能忘記關閉套接字(至少根據我的經驗)。 在你的情況下它沒有什么不同:-)
這是問題:你有這個if語句
if( req.files && req.files.product_image){
但是窮人沒有匹配的else語句:-(所以如果那個條件不正確......好吧,沒有任何事情發生。執行基本上就結束了,瀏覽器就等待了......永遠。
只需在其中添加一個帶有res.end()
的else-statment就可以了。
所以這樣的事情
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.