简体   繁体   English

Sails.js req.files为空,req.body.image未定义

[英]Sails.js req.files is empty and req.body.image is undefined

I've been trying to understand this since yesterday but I can't find any solutions, even by asking Google and StackOverFlow. 自昨天以来,我一直试图理解这一点,但是即使通过询问Google和StackOverFlow也找不到任何解决方案。 The problem is that I'm building a project inside which I have "Product" Model, Controller, and Views. 问题是我正在构建一个项目,其中具有“产品”模型,控制器和视图。 On the new.ejs view, I have a form which contains a file tag (named image). 在new.ejs视图上,我有一个包含文件标签(命名为image)的表单。 So, on the controller, I try to get the image path by logging req.files.image.path in the console but req.files is empty. 因此,在控制器上,我尝试通过在控制台中记录req.files.image.path来获取图像路径,但是req.files为空。 I also tried to display req.body.image which is undefined. 我也尝试显示未定义的req.body.image。

Here is the client-side code (views/product/new.ejs) : 这是客户端代码(views / product / new.ejs):

(This is a Sails.js app) (这是一个Sails.js应用)

Here is the client-side code (views/product/new) : 这是客户端代码(views / product / new):

<div class='container'>
    <form action='/product/create' role='form' class='form-signin' enctype="multipart/form-data" id='product-form'>
        <div class='form-group'>
            <label class='sr-only' for='name'>Name of the product</label>
            <input type="text" class='form-control' placeholder="Name" name='name' id='name' />
        </div>
        <div class='form-group'>
            <label class='sr-only' for='image'>Image of the product</label>
            <input type="file" name='image' id='image' />
        </div>
.
.
.etc...

and here is the server-side (api/controllers/ProductController.js) : 这是服务器端(api / controllers / ProductController.js):

'create': function(req, res, next) {
    // Include the node file module
    var fs = require('fs');
    console.log(req.files); //This logs : { } in the console
    console.log(req.body.image); //This logs : undefined in the console
    //Readfile with fs doesn't work cause req.files is empty
    // fs.readFile(req.files.image.path, function(error, data) {
    //  //get image name
    //  var imageName = req.files.data.name;

    //  //if enable to get name
    //  if (!imageName) {
    //      console.log('error uploading image');
    //      res.redirect('/product/new');
    //      //Else if we have an image
    //  } else {
    //      //change the path to our own
    //      var newPath = __dirname + '/assets/images/products/fullsize/' + imageName;
    //      fs.writeFile(newPath, data, function(error) {
    //          console.log(newPath);
    //          //res.redirect('/product' + imageName);
    //      });
    //  }

    // });
}

Thanks for your help. 谢谢你的帮助。

Mat. 垫。

Also available here : https://github.com/balderdashy/sails/issues/1127 也可以在这里找到: https : //github.com/balderdashy/sails/issues/1127

在您的html表单中使用method = post

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM