简体   繁体   English

表格是在输入类型=文件中没有选择文件的提交时间

[英]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.

相关问题 表单不提交输入类型:文件应该如此 - Form isnt submitting an input type:file like it should 成功提交表单后,输入类型文件未清除 - Input type file is not clearing up when successfully submitting the form HTML input type=file,提交表单前获取图片 - HTML input type=file, get the image before submitting the form 如何在不提交表单的情况下从输入字段获取文件路径? - how to get file path from input field without submitting the form? 如果未选择至少一个文件,则阻止提交表单 - Prevent submitting a form if at least one file is not selected 在不发送表单的情况下显示一些输入类型的文本,这些文本等于我的输入文件中所选文件的数量 - show a number of inputs type text equal to the number of selected files in my input file without send form 使用带有输入类型按钮的ajax提交表单输入文件类型时遇到困难 - having difficulty in submitting form input file types using ajax with input type button 带有样式的输入(类型=文件),单击时使用jquery .change事件提交,以读取所选文件 - styled input (type=file) submitting upon click with jquery .change event for reading selected files jQuery通过表单输入type =“ file”提交后检测插入的图像 - JQuery detect inserted image after submitting through form input type=“file” 如何使用AJAX将表单选择输入的值传递给php文件而不提交? - How to pass the value of a form select input to a php file using AJAX without submitting it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM